Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Gosub's Versus Functions...

Author
Message
Sharlenwar
15
Years of Service
User Offline
Joined: 4th Mar 2009
Location:
Posted: 10th Jun 2010 04:57
Okay, this has come up in a few tutorials that I have done. What is the difference between using a gosub or a function? Is there anything different memory wise, processor wise?

Thanks!
Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 10th Jun 2010 05:05
U can use a function to save a whole group of actions in one word for easier access and thats just one of its many plus
Digger412
17
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 10th Jun 2010 06:56
A function is more like a command that you're creating. Functions are blocks of commands that usually perform a recursive or isolated task that is frequently used by your program. Variables and arrays created within the function are isolated from the rest of the program. If you use a variable name of FRED in your function, it will not affect another variable called FRED in your main program, nor any other function that happens to use a similar variable name. This may seem to be a restriction, but forces you to think about cutting up your program into exclusive tasks which is a very important lesson. You can pass up to 255 parameters into your function (the only way to get an outside variable into the function, besides just calling a global one), and have the option of returning a value when the function returns. Functions that do not return a value can also be used as normal commands in your main program.

Depending on your coding style, a gosub routine can be seen as more of initialization, setup, or other less-called routines. All variables created in them have a scope that is larger that local but not global, i.e. you can't just call it from a function, that will create a different variable even if it has the same name.
Sharlenwar
15
Years of Service
User Offline
Joined: 4th Mar 2009
Location:
Posted: 10th Jun 2010 09:26
Okay cool, because from reading TDK's Tutorials I've learned a bit about functions and what not. That is a recommended read for anyone who is new. Go through each one of them!

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

Thanks for the help everyone.

Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 10th Jun 2010 22:00
Also note that Gosub's are quicker than Functions, which makes them useful for declaring variables.

So something like this:



Gosub's are best suited:



A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 10th Jun 2010 22:50 Edited at: 23rd Aug 2010 00:37
Quote: " What is the difference between using a gosub or a function? Is there anything different memory wise, processor wise?"


The main difference is that functions use local variables. Which just means that when we call a function, the variables within the function are stored upon the stack until the function completes. So they eat some memory, but only temporarily.

jfroco
14
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 10th Jun 2010 23:32
Hello,

I'm using this to disguise gosub commands as they look too 80's for me

Example:



Best regards

JF
Sharlenwar
15
Years of Service
User Offline
Joined: 4th Mar 2009
Location:
Posted: 13th Jun 2010 09:07
Well, once I got my game to the point where I will show the code, then it will be interesting to see how everyone comments. I'm looking forward to the help, as this is interesting learning everything.

nitrohaze
21
Years of Service
User Offline
Joined: 21st Apr 2003
Location:
Posted: 14th Jun 2010 15:55 Edited at: 14th Jun 2010 15:55
Another important thing to remember about Functions is that they allow you to build up a useful library of code snippets that you can use time and time again, saving you lots of work!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 16th Jun 2010 16:10 Edited at: 16th Jun 2010 16:12
I suppose it depends on your programming ethics when deciding which to use.

As an old-school programmer for over 35 years, I started when only low-level programming languages were available and when BASIC did appear on home computers it only had Goto LineNum and nothing else. Over the years I've watched as subroutines and functions were introduced to make life easier.

I believe that you choose which one to use on a 'best-suited' basis.

As has already been said, subroutines are faster, but not enough to be of any consequence, so speed doesn't really come into the decision.

Subroutines were designed to take sections of code which are used multiple times in your program and put them in one place, using them as many times as you wish. All variables in your main program are accessible and can be changed without having to specifically create global variables.

Functions on the other hand were designed to calculate a single result from a number of parameters in a 'protected' section of memory using local variables. On exit from a function, a single value is returned and all used variables destroyed.

Think of the hundreds of functions supplied with DB - those in the help files which use the '()' open/close parenthesis symbols. Every single one of them return just a single value - no more and no less.

Ever asked yourself why there's no Get Object Position XYZ() function to get all three values at once instead of having separate functions for all three?

So, I personally would only use functions for anything which needs to make changes to just a single item and subroutines for everything else.

On occasions I will also use a function which would normally be a subroutine - if the code was written for someone else (as functions can be written as self-contained entities) or I want to call the code in an If..Then statement.

At the end of the day though, it doesn't really matter which you use. I personally wouldn't use only functions for absolutely everything in my programs as the advantages are no greater than the disadvantages. I accept that some do though - it's a personal choice.

Quote: "Another important thing to remember about Functions is that they allow you to build up a useful library of code snippets that you can use time and time again, saving you lots of work!"


That's not really an advantage of functions over subroutines as the same is true of both in an include file. (When a DB program is compiled, everything in #Include files is tagged on the end of the main program so subroutines are no different to functions in this respect).

TDK

Sharlenwar
15
Years of Service
User Offline
Joined: 4th Mar 2009
Location:
Posted: 17th Jun 2010 07:21
Awesome TDK. It is great to see you post in my thread. I feel honoured. I'm pretty much working with functions, as that reminds me of C++ and other similar languages.

Anyways, I can't wait to post up some code and have you guys critique it.

nitrohaze
21
Years of Service
User Offline
Joined: 21st Apr 2003
Location:
Posted: 17th Jun 2010 15:43 Edited at: 17th Jun 2010 15:45
TDK



Quote: "Another important thing to remember about Functions is that they allow you to build up a useful library of code snippets that you can use time and time again, saving you lots of work!"

That's not really an advantage of functions over subroutines as the same is true of both in an include file. (When a DB program is compiled, everything in #Include files is tagged on the end of the main program so subroutines are no different to functions in this respect)."


In my experience when building a library of reuseable code it is better to use functions where possible over Gosubs, because you are in much less danger of encountering problems with Global Variables either affecting the sub-routine, or the subroutine impacting your main code. This is especially useful when reusing code in new projects where you could easily forget the names of all the variables you may have used in your subroutine.

You know it makes sense
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 18th Jun 2010 20:19
Quote: "You know it makes sense"


I agree entirely with your points - I was merely clarifying that having re-usable code in #Include files wasn't possible only with functions and that functions were designed for a specific purpose - with many people mis-using them because they didn't know what that purpose was.

Table tennis bats were not designed for swatting flies - but as they can be used for that purpose, does that make it right to use them?

The answer is yes of course - as long as you know what they were originally designed for. What you use to pulverize a fly with is no-one's business but your own!

So, I see where you are coming from and I 100% defend anyone's right to code in whatever style they want to.

Exclude files aside though, you do see far too many newcomers writing functions accompanied by large lists of global declarations which really should be subroutines...

TDK

luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 19th Jun 2010 01:10
It`s the same with newspaper.You can kill the fly with it or read it.If you read the newspaper or kill the fly it doesn`t matter because you know why you buy it originally.

I sometimes put some code in a function only to reduce my main loop so i can see the whole picture better.I do this now less often.Even if you use functions not only by their purpose eventually you`ll learn when is best to use them.

Coding is My Kung Fu!
And My Kung Fu is better than Yours!
nitrohaze
21
Years of Service
User Offline
Joined: 21st Apr 2003
Location:
Posted: 19th Jun 2010 12:00
Quote: "Exclude files aside though, you do see far too many newcomers writing functions accompanied by large lists of global declarations which really should be subroutines..."


Completely agree.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 20th Jun 2010 22:00
Quote: "writing functions accompanied by large lists of global declarations which really should be subroutines...""


Oops

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 24th Jun 2010 05:08
Quote: "Oops"


Not with you there...

Maybe you misread it. Maybe I could have put it better?

How about:

"you do see far too many newcomers writing functions (accompanied by large lists of global declarations) which really should be subroutines..."

TDK

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 24th Jun 2010 06:07
Lol there was nothing wrong with what you said, I was just insinuating that I am extremely guilty of it.

Gadget
14
Years of Service
User Offline
Joined: 18th Jun 2010
Location:
Posted: 25th Jun 2010 03:23
Could you give me a generic example of a subroutine please? I have gone throught the first 3 of your training posts and saw them mentioned but didn't see an example. And the actual help files are not understandable at all for me. Thanks.

Gadget
Gadget
14
Years of Service
User Offline
Joined: 18th Jun 2010
Location:
Posted: 25th Jun 2010 03:24
Could you give me a generic example of a subroutine please? I have gone throught the first 3 of your training posts and saw them mentioned but didn't see an example. And the actual help files are not understandable at all for me. Thanks.

Gadget
Gadget
14
Years of Service
User Offline
Joined: 18th Jun 2010
Location:
Posted: 25th Jun 2010 03:27 Edited at: 25th Jun 2010 08:18
Sorry about the triple post, that's what I get for doing it on my phone.

Gadget

Login to post a reply

Server time is: 2024-11-24 14:33:22
Your offset time is: 2024-11-24 14:33:22