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 / Functions VS GoSub

Author
Message
Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 15th Jan 2005 08:58
Hi all I was looking at the code from the few games I've made(all the results of tutorials) and noticed that they were all difrent in a very big way in that, oneused Functions for repetitive actions another used GoSub and the third used Goto. What my question witch of these is more efficient? Function and GoSub seem like less of a head acheto deal with than GoTo but is GoTo any faster than the others. And how about GoSubsandFunctions witch is better? Just wondering because I'm odd like that,any help would be great.

BTW Why is it that as soon as anyone(myself included in this catagory) gets DB/Pro they instantly want to make an rpg? Are rpgs just the ultimate medium of enterrtainment?

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Jan 2005 10:55
never never use GOTO. Any programming teacher will slap you for it. Gosubs are ok for many things. Functions should be used when a routine is repeatedly used many times, but with different parameters

Personally, I hate RPG's. (except zelda on NES)

"eureka" - Archimedes
BearCDPOLD
20
Years of Service
User Offline
Joined: 16th Oct 2003
Location: AZ,USA
Posted: 15th Jan 2005 13:17
GOTO=evil.

Whether you use GOSUBs or Functions is really dependent upon the situation. Usually you stick calculations in functions, a good function to develop for an RPG would be DamageCalc.
The parameters could be something like:
who is attacking, who is their target, type (magic or physical attack)

Then that function can reference statistics for each character and monster which you have stored in either arrays or types and get information such as weapons and statistics to further aid in the calculation of damage.

Crazy Donut Productions, Current Project: Project Starbuks
Sony stole our name!
Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 15th Jan 2005 13:38
Thanks guys. I new that GoTO was nasty already (from my QBasic days ) But I now have a new Q why in the heck do they have three commands if they all do relativly the same thing, and why do they have goto in the first place since it seems i'm not the only one who hates it?

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
Visigoth
19
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 15th Jan 2005 13:52
A GoTo is like a switch in a railroad track. It takes you to another spot in the code, and doesn't return you back unless you are looking for it. I'm sure there are times you may have to use a GoTO, but, generally, you can stay away from it. As for functions, usually you use them for performing calculations or string parsing or whatever. You pass in some information, and the function performs whatever function you want it to do on it, and then the function returns the result. Great for doing things that have to be done over and over. I use them alot. As for GoSubs, these are like GoTo's in a way, but, when they finish there chunk of code, they spit you right back to the next line of code. Combined with include# 's , these are great for organizing your code. If you group all your GoSub's in include files, you can call them from your game loop with one line of code. By organizing your include files for certain tasks, like Bullet Code or Score Code or Object Loading Code, you can put all your code in Subs in include files, and have a tight, easy to read game loop. It's how I do it.
Visigoth
19
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 15th Jan 2005 14:00
oh, and to answer you question about why do they have GoTo's...
Sometimes, you may need to jump past a chunk of code. Remember, the code is executed one line at a time. You may have a coding situation where you want to not run the next few lines of code, so you can use GoTo to jump past them, and those lines of code won't get executed. Just one useful way to use a goto, I'm sure there are more.
Dom
20
Years of Service
User Offline
Joined: 31st May 2004
Location:
Posted: 15th Jan 2005 21:47
There is no need to post two things in a row. Just use the almighty EDIT button


Thanks to Animeblood for the Logo!
Los
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Location:
Posted: 15th Jan 2005 22:01
Well I liked it

no no no no no no no no
Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 15th Jan 2005 22:19
HEY!!!!! You guys are messin' my topic up! LOL! Thanks for the help everyone. I think I finally get it.

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
Emperor Baal
20
Years of Service
User Offline
Joined: 1st Dec 2003
Location: The Netherlands - Oudenbosch
Posted: 16th Jan 2005 09:48
You will NEVER need a goto, every situation (well, except for OLD languages), you can avoid using it.





Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 16th Jan 2005 10:11
Hey everybody look whar I made! I made a crappy li'l program that uses goto!


It's the first thing I've made without a tut, so I'm real excited about it......NOT!LOL
If you play it you'll see what I mean.
It took me about two hours to make it so it's a pretty half baked quikie job tell me what ya think about it though, if you want to.

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
Cryptoman
20
Years of Service
User Offline
Joined: 24th Nov 2003
Location: Utah Mountains
Posted: 16th Jan 2005 15:06
There are sometimes you may need a goto, sometimes logic don't pan out, so instead of making five different funtions cause something didn't pan out, you may just want to pass some code or start it all over.

There is also overhead to think about, a gousub or a funtion call has to store where is was called from, that takes more cycles than a simple goto in certain situations.

Its all a matter of opinion, and teachers, well they just know how to read a book. From 1989 to 1991 my coding teacher could never write anything that ran faster in Microsoft basic or assemly than I could. Even that, I still only made a B average, because I would not follow the so called text book style.

Make your own style and if you find something better then change, else don't worry about it.


Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 16th Jan 2005 16:27
Here Here! Thats what we need more individualism(SP?) thats the reason we're all using the same program isn't it?

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
MDTalley
19
Years of Service
User Offline
Joined: 13th Jan 2005
Location: Mobile, AL
Posted: 18th Jan 2005 06:27
My 2 cents.

Functions that don't return values are, by default, subroutines. So it really doesn't make any difference whether you call a function or subroutine from a compiler standpoint. From a code-readability standpoint, you know if you call a subroutine, no value will be returned, so it can be a good coding practice (in Dark Basic, anyway) to only call functions when you expect a value to be returned.

As for GOTO's, I generally sympathize with the group sentiment to never use them. There is always a better way, which is why any mature language doesn't have them (or label either for that matter). The only possible exception to this could be if you have "Exit the Program" logic you put in a subroutine, that you never expect to return from. It would be permissible, but not necessary, to GOTO there.
Ralen
21
Years of Service
User Offline
Joined: 22nd Jul 2003
Location:
Posted: 19th Jan 2005 03:23
The reason you don't use goto is the same reason you don't put END in your program in 5 differant places :p. Its just bad programming to use it. It basically breaks the flow of the program.

Gosub vs functions is situational. Except as far as good programming goes you use either one or the other not both. It helps keep your code readable and uncluttered.

Ralen
TravisP
20
Years of Service
User Offline
Joined: 30th Jun 2004
Location: Behind you, with a knife!
Posted: 19th Jan 2005 03:50
Quote: " You will NEVER need a goto"

Think this...

If I were looking for a blank array to store my varible in which would be faster? Sure you use like "and found=0" but it still would have to loop that 999 times. This is just one example.

Note: The above I didn't say, your just crazy.

Not a ripoff of The Twilight Zone
Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 19th Jan 2005 10:56
Thanks all.

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
ReD_eYe
21
Years of Service
User Offline
Joined: 9th Mar 2003
Location: United Kingdom
Posted: 19th Jan 2005 20:42
@Travis P, Just set the variable you are looping to the maximum value and the for next loop will exit.


Or if you are using functions, just stick an "ExitFunction ReturnValue" in to your for-next loop when you've found what you want.
Like so:

It would be crazy to loop 65000 times whenever you wanted to find a single un-used object. So you exit the function when you find it and problem solved.


http://www.dbspot.com/NYC
New years competition... Fo Shizzle.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 19th Jan 2005 21:41
Quote: " @Travis P, Just set the variable you are looping to the maximum value and the for next loop will exit."

Well, I don't think that this makes the code more readable than the one with the "goto/label" combination.
A lot of languages have something like "break" or "exit" (some even with an optional label, ie JavaScript) which is basically the same as "goto". As with almost everything, use it sparingly and wisely and "goto" can be a very good friend.


Play Nice! Play Basic! Version 1.06
MDTalley
19
Years of Service
User Offline
Joined: 13th Jan 2005
Location: Mobile, AL
Posted: 20th Jan 2005 03:27
<Applying the paddles to the dead horse, so he can revive it and beat it some more>

Break and exit are not the same as a GOTO. They simply drop out of whatever loop or execution they were in, and go to the next line of code in the program flow.

General rule of thumb is, if anybody is using GOTO's to control program flow, they need to educate themselves on Structured Programming Techniques. Something, I've sadly found out, they no longer seem to teach in schools.
Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 20th Jan 2005 03:51
Hi! I don't want to get my butt burned for having to topics running at the same time so I'll post my question here.

Alright, I'm currently prototyping(SP?) a third person camera just for the heck of it and was wondering what a good poly count for weapons would be if I had maybe a 1000 poly main character glued to the screen? These weapons would be anything from guns to swords to crowbars(Because they're so cool!) I've bein making a pistol model and it's about 145 polys is this to much? Just asking, thanks.

-Thomas

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.
John H
Retired Moderator
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 20th Jan 2005 04:17
Functions are in many ways more flexible and easier to use then for the pure reason of being able to define only the necessary information. I commonly use functions for RPG elements such as dialog Im a big function guy, Ill use functions for anything The only time I ever use gosubs is for media/main sections of code (if I had to change resolution, you have to reload all media)


Click here to join our forums and get updates on game progress sooner!
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 20th Jan 2005 07:26
Quote: "Break and exit are not the same as a GOTO. They simply drop out of whatever loop or execution they were in, and go to the next line of code in the program flow."

Which comes to the same result in the above examples.


Quote: "General rule of thumb is, if anybody is using GOTO's to control program flow, they need to educate themselves on Structured Programming Techniques. Something, I've sadly found out, they no longer seem to teach in schools."

Agreed.


Play Nice! Play Basic! Version 1.06
Spaceman Spiff
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location: Smacking my head on your keyboard.
Posted: 20th Jan 2005 09:39
Thanks, Empty and RPGamer.

I've bein driving since the age of three; if you don't belive me ask my parents, you can find them at the county hospital for the mentally imbalanced.

Login to post a reply

Server time is: 2024-09-23 10:13:58
Your offset time is: 2024-09-23 10:13:58