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.

DarkBASIC Professional Discussion / subroutines vs. functions

Author
Message
relyks
23
Years of Service
User Offline
Joined: 10th Apr 2003
Location:
Posted: 15th Jun 2003 08:01
Is there any advantage to using functions over subroutines besides being able to use functions in multiple programs? Are functions maybe faster for some reason?
Guyon
23
Years of Service
User Offline
Joined: 6th Jun 2003
Location: United States
Posted: 15th Jun 2003 08:54
From what I understand you can build a function and save it. Then use and #include to bring it into any of your programs. You can not do that with a subroutine.

Otherwise: Functions are best used to send a value to and have something returned. Like a math function. Subroutines then can be used for everything else. While both can be used almost interchangeably, make your own style and stick to it for clarity of your work.
n3t3r453r
23
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Russia
Posted: 15th Jun 2003 11:58
> make your own style and stick to it for clarity of your work
That's right!

I'd like to change the world, but God doesn't want to give me sources!
Rob K
Retired Moderator
23
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 15th Jun 2003 12:27
> You can not do that with a subroutine.

Subroutines can be accessed from other include files as well.

Technically, I believe that subroutines may process one or two less instructions (not completely sure though), however the difference is negligable.

Use the method that suits you. Personally I find functions neater and easier to work with.

Do you want Windows menus in your DBP apps? - Get my plugin: http://snow.prohosting.com/~clone99/downloads/tpc_menus_103.zip
Shadow Robert
23
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 15th Jun 2003 13:33
Subroutines - Static Global Functions
Functions - Variable Data

although personally i'll almost always go for functions, there are sometimes a good call for Subroutines - and DBP's architecture means that #include files work just like they do in other languages so you no longer have to think of them as just pure function plugins.

the way i tend to use Subroutines now is a little more creative than i used to...

something like

the function


the subroutine


certainly is just a waste of a function... remember that subroutines are just code jumps rather than seperate entities, this means that everything that you can use in them can be already declared and you don't have to worry about new instances being created.
The only real reason to use a function in that case would be because you require multiple control instances - even then just changing who uses the standard x,z,s values would be simpler and quicker.

however Subroutines are actually a little more versatile than you'd think for example



you couldn't do that just using functions, and certainly not as simple or cleanly anyways
Anyone who uses C++ will not doubt dawn that they actually use SubRoutines alot within thier Class Function hehee, as DBP uses then the EXACT same way as C++ does. Makes them alot of fun to use.

you can also have multiple Subroutines with the same name in this fashion - perhaps when they add "Static" as a variable type for functions, so we can have a Local but Constant Variable until the program ends

Within the Epic battle of the fates the Shadow and the Angel will meet. With it will harbinger the very fight of good vs evil!
the_winch
23
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 15th Jun 2003 18:47
Another thing is that if the ide is ever fixed you will be able to fold the functions so they only take up one line. Making reading and editing the code a bit easier if you are using functions.
Shadow Robert
23
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 15th Jun 2003 23:41
Winch if Guy wanted to he could add that feature for GoSubs as well, i mean when you enter the "Endfunction" keyword it no doubt just searchs upward for a Function statement.
i mean the same thing cound be use for gosub returns.

no doubt was/is a planned feature ... and ya know we've been waiting bloody month for this Guy interview about the new IDE on DarkBasicPlanet... when the heck is that actually going to happen. I kinda wanna know what the heck is going on with it, what we can hope to expect and such.

Within the Epic battle of the fates the Shadow and the Angel will meet. With it will harbinger the very fight of good vs evil!
Shadow
23
Years of Service
User Offline
Joined: 17th Oct 2002
Location: In the shadows
Posted: 15th Jun 2003 23:51
Possible solution:

Use a function if the process contained within is variable.

If the process is identical every time, use a subroutine.

Also: could the choice of function/sub affect program speed?
Witch Bomber
23
Years of Service
User Offline
Joined: 25th Jan 2003
Location: Scotland
Posted: 15th Jun 2003 23:57
Quote: "i mean the same thing cound be use for gosub returns."

The only problem though is that a label is declared by a string followed by a colon. It would be difficult to make it search upwards for a colon because then it might find a line of code with more than one command and think that is a declaration.

Quote: "No sober man dances unless he happens to be mad"

If that is the case, what happens when a madman gets drunk?
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Jun 2003 00:03
Ah, it's probably not general knowledge, but Insider had an argument with a car, and came off a little worse for wear. Hopefully he'll be back on his feet soon.

Back on topic again, the difference in speed between a function with no local variables and a subroutine is minimal. It's so little in fact that I generally don't worry about it when I'm optimising code.

If you look at code I post, you'll see that I almost never use subroutines, and when I use labels, it's usually as a target for a 'restore' command rather than goto or gosub.
Shadow Robert
23
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 16th Jun 2003 00:13
lol i'm always kept outta the general knowlage loop ... but insiders' been on MSN daily - wonder why he never said anything


Quote: "The only problem though is that a label is declared by a string followed by a colon. It would be difficult to make it search upwards for a colon because then it might find a line of code with more than one command and think that is a declaration."


you'd be looking upwards for [string]: ... if anyone uses the ':' nexto thier text then DBP's Compiler things its a Label - so i doubt there would be that around.

But then again he could always achieve it by once a Value is declared as a label - then checking each line for a return, if you think about it he already lists all of the labels you use so obviously it isn't that hard

as for Subroutines as a whole it is just a matter of coding style more, but there are a few tricks they and achieve which Functions just can't.
I mean i don't use them much, but LeeBamber seems to love using them - just look at the example for both DB and DBP.

Within the Epic battle of the fates the Shadow and the Angel will meet. With it will harbinger the very fight of good vs evil!
Vandetta
23
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 16th Jun 2003 00:42
You can make "fake" classes in DBPro? Can someone post a snippet of how to make a class?

Login to post a reply

Server time is: 2026-07-11 14:56:19
Your offset time is: 2026-07-11 14:56:19