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 / When to use functions vs. GOSUB routines?

Author
Message
Alaror
13
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th May 2011 18:26
Back again with another basic question. From what I understand user defined functions and GOSUB routines can pretty much achieve the same thing (feel free to correct me if I'm wrong). With that said, using too many GOSUB routines would probably make the code more difficult to debug since it forces you to jump to another part of your code if something isn't working.

Are there specific things each should/should not be used for? For example, I'd imagine that using GOSUB would be good for making menus while functions would be better for repeated actions like moving a character. I'm trying to get into good habits now so your help is much appreciated
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 26th May 2011 19:58 Edited at: 26th May 2011 20:33
The difference is that variables inside functions that aren't set to global do not retain their values outside of functions... which is a plus if the variables are only temporary anyway.

It's really not more difficult to debug because modern IDEs have the ability quickly jump to the start of functions and labels.

I personally only use functions and tolerate using GOSUB when helping newbies with their own code.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th May 2011 20:05
Functions when different parameters are used to affect a particular set of instructions. I use subs to help separate blocks of code to make things easier. Functions for chunks of code that may be used more than once or to do calculations that return a result.

Jimpo
19
Years of Service
User Offline
Joined: 9th Apr 2005
Location:
Posted: 26th May 2011 20:48 Edited at: 26th May 2011 20:58
Functions can take in values and return results, so if you need either of those properties, you want to use a function. Besides that, it's all up to personal style when to use which.

I personally do things like Phaelax described, gosubs to separate sections of code and functions for everything that needs to take or return a value.

Here's some examples:




As you can see, functions are typically very specific actions and require specific values to work or need to return a result. Gosubs just separate large sections of code.

Alaror
13
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th May 2011 23:24
Great, this is exactly the information I was looking for!
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 30th May 2011 17:39
I use functions for everything. In my world GOSUBs are as bad as using GOTOs...

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 31st May 2011 02:33
Gosubs aren't bad, infact they're very useful. With my game engine, I manage my code using multiple .dba files. Mouse code goes into mouse.dba, keyboard code goes into keyboard.dba. If I'm working on multiple projects, then each project can use the mouse.dba file, saving myself from rewriting and duplicating code for diffent projects.

For this technique to work well, each dba file begins with a gosub which prepares variables, user types and arrays, such as Sub_Mouse_Ini. Then, I write out all of the functions that are required to work hand in hand with that specific dba file & gosub.

Now, in my main dba file, to get a fully functional mouse, all that's required is for the mouse.dba file to be added to the DB Project, then a call of the Sub_Mouse_Ini. Then the mouse functions as required.

Not too sure if my technique is the right way, but it works really well!

For those against using gosubs, please advise how you would manage multiple .dba files with out using them.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 31st May 2011 07:19
Quote: "I use functions for everything."

You must have a lot of globals then

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 31st May 2011 08:55
Like using a spanner to hammer some nails in.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 31st May 2011 11:27
Quote: "each dba file begins with a gosub which prepares variables, user types and arrays"

Can be done with a function just as easily...

And yes I have alot of globals, but they are contained to the module they are used in by a PREFIX... like ENT_ for entity stuff. I usually make functions to access module variables and arrays.

Now I'm not trying to forbid GOSUBs, I just feel that you can get a more readable structure if you stick with functions. I don't know if there are any speed-benefits to using GOSUBs, so that may be another point to examine.

I guess my preference comes from my C/C++ coding style.



baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st May 2011 15:01
Looks pretty neat to me... using UDT's like that is quite tidy and easy to understand.

Globals aren't bad as long as you can keep them organised. That's my usual problem. I relearned programming using a language that keeps all variables global unless specified as local and I learned some bad habits from it. I just about have myself free of those habits now as I've been using C# for over a year.

Lonnehart
15
Years of Service
User Offline
Joined: 17th Apr 2009
Location:
Posted: 1st Jun 2011 02:39 Edited at: 1st Jun 2011 02:41
I guess I should consider using subs now. I've read on some of these forums that using subs was just as bad as using GOTOs and that I should never EVER use them. After this post I'm going to reconsider...

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jun 2011 10:40
GOSUB is somewhat safer than GOTO because it requires the 'return' statement. This means you will always return to the point in the code you called it from after the subroutine has finished. GOTO does not return so it is very easy to add memory leaks and cause all sorts of confusion in your code.

Using a subroutine is no different to using a function (basically) except that you cannot keep variables 'local' to the routine so you have to be more careful with what you call your variables.

enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 2nd Jun 2011 19:48
Are variables local to the file they're in?
If you GOSUB to a subroutine in another file, will the variables there be separate from variables in other files?

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Jun 2011 22:26
Quote: "Are variables local to the file they're in?"

No. Variables used in subroutines are not local simply because they are in a seperate dba file. I wondered about that too and tested it a while ago.

Using seperate dba files is just another way of organising your code, so for example you could include a dba from a common location which contains functions and/or subs that you use on a regular basis in multiple projects.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 3rd Jun 2011 00:28
Yah, as with my example. It's fantastic to have subroutines to jump to different DBA files to declare a bunch of stuff, then return to the main dba file to continue on with other subroutines, which again, jump to the relevant DBA file.

Eventually, your code with hit your game loop which will contain your functions. These functions can be stored in the relevant DBA file which also contains the subroutine which declared the user types and globals that they're referencing.

And with respect to this technique, you might be able to code this way without actually using subroutines, but when I tried to check this, things crashed and burned pretty quickly. Subroutines allow a clear order to exist, rather than assumed order, when using multiple DBA files.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 3rd Jun 2011 06:04
I personally hate using different .dba files in one project. All my code is only in one .dba and it's just as neatly organized because of the ability to jump to any function you need to via the IDE. It's also easier to debug because you know if it causes an error on line 2013 you can actually go to that line number because you're only using one .dba file.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 3rd Jun 2011 08:46
That point I can definitely agree on. Troubleshooting a bug whilst using multiple .DBA files is an absolute chore. You really have to keep your finger on the pulse and know what's going on when coding with multiple DBA files due to this problem.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Jun 2011 11:25
Quote: " Troubleshooting a bug whilst using multiple .DBA files is an absolute chore."

I have never had a problem. In fact I find it easier as when I have a display problem I look in my "display.dba", if loading media I look under "setup.dba" and there's always the "Go to project line" when an error tells you what line it occurred on...

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 3rd Jun 2011 11:29 Edited at: 3rd Jun 2011 11:30
Does it really matter? DBP usually highlights the line with the error; and for when it sometimes doesn't, it joins all the .dba's together and then outputs that as filename.dbsource; It's a simple job to open this and just jump to the line number it's reported the error on to find out where the error is.

And for the record, I tend to organise all my projects like this...

main.dba


moduleName.dba


Just my opinion tho, I have no formal training or anything; this is just what works for me

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 3rd Jun 2011 18:26
Quote: "Just my opinion tho, I have no formal training or anything; this is just what works for me"


That's how most of us learned... school of hard knocks and buggy code.

Tom0001
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location:
Posted: 3rd Jun 2011 22:14
In my opinion, functions are better by far -- while you can still return stuff when using a gosub, functions are much better for this as you can overload them with values for certain operations and then have a result returned. While both can be called multiple times for the same result, functions are better for this.

For example, functions can be used to set arrays easily with something like:


Whereas with gosub, this would prove quite more tedious -- it doesn't have to be for setting arrays, of course, it always returns the value you want and obviously has many applications that gosub CAN accomplish but isn't as efficient in doing so.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Jun 2011 04:08
Quote: "Are variables local to the file they're in?
If you GOSUB to a subroutine in another file, will the variables there be separate from variables in other files?"


I believe that included DBA files are merely appended to the end of your main file during compile time, so it's really just one big file in the end anyway.

Tom0001
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location:
Posted: 6th Jun 2011 09:23
Phaelax is correct. When you use #INCLUDE, it's essentially the same as just copying the code from that file and dumping it onto the end of your current file. If you wanted, you could have one file handle setting up the game, the next file handle running the game and so on, like a normal program. Personally, I just use external DBA's for specific functions.

Login to post a reply

Server time is: 2024-11-22 22:35:09
Your offset time is: 2024-11-22 22:35:09