Computer Science Canada

Functions

Author:  Piro24 [ Sat Dec 09, 2006 12:12 am ]
Post subject:  Functions

Ok, I have a question about functions, and don't say read the tutorial, because I did and I don't get it still, hence this is why I'm asking.

I made a program which converts binary into decimal, and I want to know how to properly put it into a function. The converter is simple:

code:
for i : 1 .. length (binary)
    if binary (i) = "1" then
        decimal += 2 ** (length (binary) - i)
    end if
end for


I just want to properly incorporate it into a function which I can call upon for the result of binary to decimal.

Author:  Cervantes [ Sat Dec 09, 2006 8:53 am ]
Post subject: 

It would be something like this:
code:

function bin_to_dec (bin : string) : int
    var decimal := 0
    for i : 1 .. length (bin)
        if bin (i) = "1" then
            decimal += 2 ** (length (bin) - i)
        end if
    end for
    result decimal
end bin_to_dec


: