Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Functions
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Piro24




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat Dec 09, 2006 8:53 am   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: