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 / Function vs GoSub

Author
Message
Infinity8
13
Years of Service
User Offline
Joined: 27th Feb 2011
Location:
Posted: 28th Feb 2011 17:37
So, my question is this:

Which method is faster, calling a function or the gosub/return combo?

also are there any specific pros or cons for either of them?

Just want to know so that i can get in the habit of using the best one.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2011 22:43
I don't think either is faster. They each have their uses.

I tend to use a function when I might want to pass a few specific variables to the function for one return or a specific action.

I would tend to use gosub for multiple returns or when I need to define UDT's (User Defined Types).

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 28th Feb 2011 23:04
The best things about using functions is that you don't see unforseen bugs when you use the same variables. If you don't have them defined as global all temporary variables you use in functions will not be carried over to the main program. I personally only use functions.

Infinity8
13
Years of Service
User Offline
Joined: 27th Feb 2011
Location:
Posted: 28th Feb 2011 23:21
ahh okay, thank you

I personally have been using functions for a while but i read on one of the threads that someone claimed gosub was significantly faster than calling a function, so i just wanted to make things clear :p
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 1st Mar 2011 10:32
I've tried both functions & go subs, and functions win for me every time, so much so that I never use go subs in anything I write. Still, I'm probably missing an obvious example of where a Gosub is superior to a function, so I'd appreciate for anyone to show me an example as such.

Help build an online DarkBASIC Professional help archive.
[href= http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki]DarkBasic Help Wikia[/href]
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Mar 2011 10:36
Quote: "I've tried both functions & go subs, and functions win for me every time, so much so that I never use go subs in anything I write. Still, I'm probably missing an obvious example of where a Gosub is superior to a function, so I'd appreciate for anyone to show me an example as such."

I use a gosub for my setup routine which I use to load media and set up constants, globals and UDT's. I seperate my setup code to keep my main dba fairly simple and clutter free.

You can't create UDT's in a function.

It's about the only reason I use them...

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 3rd Mar 2011 01:54 Edited at: 3rd Mar 2011 03:23
! Baxslash! You've just solved one of my work flow issues!

I've been dumping my functions into individual DBA files, then referencing each DBA file to the project. Problem being though, I couldn't host the Dim Array or Dim Type stuff that each function would manipulate, meaning that my DBA library wouldn't be a "complete" package as such, requiring all the Dim's that each function was using to be made in the main DBA file.

BUT NOW, I've just realised I can create a DBA file, have a "Go Sub" for the first half of the file, where all the specific arrays are dimensioned, then, the function part follows after the sub routine ends. This results in a bunch of "Go Subs" being called prior to my game loop, rather than all the messy dimming code.

I think this will work. I'm gonna try it now!


Fingers crossed it all works!

EDIT: Damn it, doesn't totally work.

I can get this working;



But not this;




EDIT AGAIN!: Woo! Problem solved by using sticking a Global infront of everything.



EDIT AGAIN:

ZOMG THIS IS THE BEST WAY TO CODE EVER! EVERYTHING IS SO ORGANISED, IT'S AWESOME!!!!!!!

Ehem. So. I wonder if my adventures put this long debated topic to rest. With both the power of functions and go subs, you can achieve anything!

Help build an online DarkBASIC Professional help archive.
[href= http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki]DarkBasic Help Wikia[/href]
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Mar 2011 10:26
He he

Glad I could help!

I always use either sub-routines or functions in appropriately named dba's "Utilities", "Display", "TBM" etc. It keeps my code in some kind of logical filing system instead of a huge long stream of code.

Quel
15
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 3rd Mar 2011 20:50
Personally i think having functions their own variables, and your need to globalize everything to be able to read there is the stupidest thing ever.

I have never used this feature of functions, it is always a pain in the ass to globalize everything, and hunt down the one last F-in variable which hasn't been yet globalized SO i can work with functions.

Why can't it be reverse?... to declare what variable i don't want in the functions? It would be so much better.

The current state just leads to a bunch of totally unnecessary mess.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Mar 2011 21:10
Quote: "Personally i think having functions their own variables, and your need to globalize everything to be able to read there is the stupidest thing ever."

There are several reasons why you are wrong but the main one is that local variables won't mess your code up elsewhere without you realising it. You only need to set variables as global if they are needed throughout the code and cannot be propogated easily. It's fine for you to program however you want but don't dismiss something because you don't understand it, that's kind of the worst thing any programmer can do.

What you should do is learn the best way to use functions and take advantage of their pros rather than dismiss them because of their cons.

Quote: "Why can't it be reverse?... to declare what variable i don't want in the functions? It would be so much better."

That's exactly what you can use functions for! If you don't want a variable to be propogated into other parts of your code then don't declare it global and hey presto... it won't turn up where you don't want it to. Without functions there is absolutely no need for the "global" command (it's the only case it works under as far as I'm aware)

Quote: "The current state just leads to a bunch of totally unnecessary mess."

I don't find that at all, in fact I find DBP a very easy language to keep tidy. Maybe you should review your current coding practices?

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 3rd Mar 2011 21:17
Baxslash, note the global declaration in my sub routine, along with the behaviour it had on my program, in that, with out the global declaration, the variable was never declared. Interesting stuff...

Help build an online DarkBASIC Professional help archive.
[href= http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki]DarkBasic Help Wikia[/href]
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Mar 2011 10:13
Quote: "with out the global declaration, the variable was never declared."

Do you mean you couldn't access "Mouse" in another function? That would be what I expect as it would only be declared locally (in the subroutine or any code it returns to). If you mean it would not show up in the code it returns to I would be surprised...

As far as I'm aware using sub-routines do not create a 'local' environment, just a sidestep and then step back into your current environment using all the same available variables. Using functions takes you into a new 'local' environment each time meaning the function only recognises what is passed to it or declared internally in the function (or any global variables). This means you can use any none global variable name which will start with a null value.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Mar 2011 14:22
@baxslash,
Quote: "I use a gosub for my setup routine which I use to load media and set up constants, globals and UDT's"

Setting globals (not declaration of globals, unless you set them at the same time), fine.
UDT's and constants do not need to be 'executed'.

In the case of constants, you can declare them anywhere in your code.
UDT's should be declared outside of functions, and 'earlier' in the code than where they are used (you can sometimes get away with declaring them anywhere too, but declaring them earlier works all of the time).

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Mar 2011 14:33 Edited at: 4th Mar 2011 14:35
Hi IanM

Quote: "Setting globals (not declaration of globals, unless you set them at the same time), fine.
UDT's and constants do not need to be 'executed'."

I declare then set constants and globals (only set when necessary) in my setup routine, and only define UDT's in my setup sub-routine.

Does it cause a problem if you declare a global in a sub-routine when you aren't setting it's value? I'm confused...

Quote: "In the case of constants, you can declare them anywhere in your code.
UDT's should be declared outside of functions, and 'earlier' in the code than where they are used (you can sometimes get away with declaring them anywhere too, but declaring them earlier works all of the time).
"

The problem with declaring them anywhere in your code is remembering when you declared them, that's where it can get messy and that's why I set all of mine up in a 'setup' sub-routine at the very start of my code.

I haven't yet noticed any problems with doing this.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Mar 2011 19:35
No problem with doing it, just that it's not necessary.

For example, the following pieces of code will give identical results:


and ...


In fact, I've posted about 'modular' programming (DBPro style) fairly recently here: http://forum.thegamecreators.com/?m=forum_view&t=180209&b=1
Have a read of everything in there and see if anything there is useful to you. For anything that I posted there, try them out and then take the bits that work for you.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 4th Mar 2011 21:57
Ergh. Back to the drawing board. This simplifies, but changes everything!

Will give the techniques discussed here a go shortly. Early testing shows it's all good.

Help build an online DarkBASIC Professional help archive.
[href= http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki]DarkBasic Help Wikia[/href]
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Mar 2011 19:01
I see, thanks IanM! I thought you were saying I'd done something that wouldn't work.

I guess I just like organising all that stuff into one sub-routine when possible.

I'll have a proper look through your link when I have more time!

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 6th Mar 2011 07:55
Ok, did some testing, but found that without the Go Subs to initialize things, I lost the ability to control how my program initialized. So with that, I'll keep using Go Subs to access and initialize other Dba files which are storing specific functions too.

Help build an online DarkBASIC Professional help archive.
[href= http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki]DarkBasic Help Wikia[/href]
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 7th Mar 2011 23:18
Quote: "For example, the following pieces of code will give identical results:"


Well, now that is quite interesting! So, just like I would put my DATA statements at the end of my code, I can put my constants, globals, and UDTs at the end, and not even have to execute them?!

I think I'd still put a label on them, though, it just makes it easier to jump to them in the editor.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th Mar 2011 18:09
Missed your post, but I'll answer now.

Constants: Yes.
Globals: Yes, but only if you initialise them to 0 or empty strings.
UDT's: No. They should be defined in front of the code that uses them. Read my posts in the link I provided to see how I deal with this.

Quote: "I think I'd still put a label on them, though, it just makes it easier to jump to them in the editor."

That's a reasonable thing to do, but it may also be a sign that your modules are too large - I'll leave that to you to determine.

Login to post a reply

Server time is: 2024-11-22 23:23:51
Your offset time is: 2024-11-22 23:23:51