SRL Archive - OSR Help

Procedures vs. Functions? Practical Use


toto89

2012-03-07 15:29:21

I've been mapping out a script (my first script) and I'm trying to determine what would be practical; a procedure, or a function?

I know the difference between a procedure and a function (minimally). A procedure is basically a function without the return value. My question to Villavu is: If I'm making a Runescape script, when would a function be better to use than a procedure?

I'm aware that both can be used pretty much interchangeably, so, when would a return value be more useful than no return value?

All answers are appreciated.

Texy

2012-03-07 15:43:48

Most times, a return value isn't needed, so you'll probably stick to using procedures.

Geo

2012-03-07 15:47:17

A function would be better if you're trying to achieve a result, example banking would be a function as it would return a result of whether the BankScreen is open which would return a true result and if the BankScreen isn't open then the result would be false meaning you could loop back to the start and begin the banking function again.

A procedure is good for something that doesn't need a result. For example antiban would be a procedure because it wouldn't need a result as all it's doing is moving the mouse or random right clicking somewhere.

Brandon

2012-03-07 15:47:44

From: Texy
A function is much more limited than a procedure… In most cases you'll probably be using procedures.


Don't ever say that..

A Procedure IS a function that has no return value.. aka VOID if u think of a C language..

Functions return a value that you can use.. procedures don't.. they just do the same thing a function does but gives u nothing in return.


For example, I'd use a function to figure out whether something was done or not.. and for my main loop I'd use a procedure because I don't need to know anything from it..

Function OpenBank: Boolean… it returns a boolean.. aka true if the bank is open.. false if it isn't.. then u can use it to repeat until it returns true aka opened..

whereas

Procedure DoMainLoop;
//Just loop everything over and over.. no need for a return value because we know it's looping.

All in all both functions and procedures should be used when you need to use it's body of code over and over.. if your only going to use it once, no point in making it a function/procedure.

masterBB

2012-03-07 15:48:15

From: Texy
A function is much more limited than a procedure… In most cases you'll probably be using procedures.


That is not true. What limit are you talking about?

Use functions for methods that can go wrong, like finding a monster or walking to a spot.

Use functions for methods that return a certain state of an object. Like getting your health or the amount of trees on the minimap.

Use functions when it will save you from one global var. Like loading a dtm.

Use procedures on all other cases.

Texy

2012-03-07 15:49:05

From: ggzz
Don't ever say that..

Yeah I didn't mean for it to come out that way, and I edited my post right after I realized.

toto89

2012-03-07 20:30:47

Well, I appreciate everyone explaining the practical differences between Procedures and Functions. Hopefully newer scripters like myself will be able to learn from this question. Now, when I go to do my banking, I'll know to use a fuction. Just because I will be able to have a return value to check and see if the bank is open or not.

Flight

2012-03-07 21:41:44

Functions are really the way to go, especially for making sure everything within it was done correctly. Absolutely a MUST for fail-safes.

rice

2012-03-12 22:07:59

Functions are best used for fail safes and making sure a certain procedure is done and if it isnt you can make it repeat or use another function to make that task work.

sahibjs

2012-03-12 22:37:54

Think about it this way.

A irl procedure would be throw away the trash. thats basically as far as it goes.

A irl funtion would be throw away the trash, and then check it its thrown away or not, because it would return as true or false (boolean). I would stick to functions as much as I could.

smurg

2012-03-13 02:30:31

It's probably even helpful as a learning process to make everything a function with a output. You don't necessarily need to use the output, but you need to debug later, it would make it a lot easier.

Abu

2012-03-13 19:05:49

Yeh think of it this way. If I'm walking to an area using a procedure, then I won't have a result to tell me if I have made it there.

If I walk to an area using a function, then as the result I can put in the area I want to walk to.

This way if the result of my script walking to an area has not got me to the area, it will return false. – Its basically a failsafe.


Kind of anyway… :p