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

Username:   Password: 
 RegisterRegister   
 [Tip]Functions or Procedures?
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Fri Dec 29, 2006 10:02 am   Post subject: [Tip]Functions or Procedures?

Understanding the Difference:

Procedures:
Procedures are blocks of code, that when called, executes. A procedure can take any number of arguments (variable or not) and use them to do just about anything.

Functions:
Functions are like procedures in that they are blocks of code that executes when called, and they can also have any number of arguments. They compute a value and return it.

Difference:
Functions return a value, procedures don't.

Who Cares?:
You should. Procedures modify the state of the program. While this may not sound bad, it is. Think about it, when you use randint(), do you really know what's happening to your precious variable inside? However, using the function Rand.Int(), you don't need to worry about what's happening to your variable, because you know exactly what's happening to it. Rand.Int() would be computing a new value, and you assign that value to your variable. Generally speaking, using a procedure to modify one of your variables is considered bad coding practice, because of the reasons mentioned above. For those reasons, procedures should be avoided when modifying variables.

Examples:
Take the following trivial example into account if you still don't believe me (and others):

Turing:

procedure put_5
    put 5
end put_5

put_5


Now, you see that it works, that's all fine and dandy...or is it? Here we have a procedure that modified the global state of the program, and as I mentioned above, that is generally regarded as bad coding practice. What if you weren't expecting that put in that procedure? Now you're screwed because your format was all based around that procedure not having a put in it. However, this can be rectified using a function:

Turing:

function put_5 : int
    result 5
end put_5

put put_5


This is better isn't it? Now we don't need to care what goes on inside the function, all we care about is the value it returned. I know that this may seem like an extremely trivial example (and it is), but this applies to all things.

Conclusion:
If ever there comes a time when you could choose between a function or a procedure, take the function over the procedure.
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Sun Jan 06, 2008 6:33 pm   Post subject: RE:[Tip]Functions or Procedures?

you forgot to give your function the spec, int.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: