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 / The GOTO and GOSUB command

Author
Message
Nilrem
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: United Kingdom
Posted: 18th May 2003 12:15
What is the/are difference/(s) between the GOTO and GOSUB command, because up to now I've always used the GOTO command.
I hear and I forget. I see and I remember. I do and I understand.
Barney
23
Years of Service
User Offline
Joined: 18th May 2003
Location:
Posted: 18th May 2003 12:30
GOTO will jump to another point in your code where it will continue with the execution

GOSUB (GO SUBroutine) will make the same jump and will continue execution until a RETURN command is encountered, after which the execution will continue with the line of code following the original GOSUB statement. Look at the GOSUB as a very simple function call that does not return any results.

BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 18th May 2003 12:48
You should get in the habit of using GOSUB's or Function Calls.

GOTO's make for messy, harder to read, harder to support code.

You won't find many professional programmers using many GOTO's if any at all, which should indicate that as you become a better programmer, you will be using GOTO's less and less.

Hope this helps.

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th May 2003 13:40
1968: Edsger Dijkstra introduces the concept of structured programming and declares that GOTO statements should no longer be used.

This guy was so ahead of his time that he wasn't allowed to put "Computer Programmer" as his profession on his marriage certificate.

Here's Java as it was portayed in 1973 by the man himself...

http://www.cbi.umn.edu/collections/inv/burros/ewd594.htm

Thanks in advance.
All the Best,
StevieVee
Nilrem
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: United Kingdom
Posted: 18th May 2003 14:24
Ok, thanks for the responses; I will try to use gosubs instead of gotos.

I hear and I forget. I see and I remember. I do and I understand.
Kangaroo2
23
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 18th May 2003 14:38
A got is sensible to use, if you only need to skip to an area and don't need to be able to return to exxactly the point the program left off, for example "if mouseclick()=1 then goto level1" but gosubs can return from wherever you sent them, like functions.

Coming Soon! Kangaroo2 Studio... wait and quiver with anticipation! lol
[email protected] - http://www.kangaroo2.com - If the apocalypse comes, email me
Nilrem
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: United Kingdom
Posted: 18th May 2003 16:26 Edited at: 18th May 2003 16:26
Yes Sam, I'm now using functions, gosubs, and gotos in my code now (where it is needed).

I hear and I forget. I see and I remember. I do and I understand.
Rob K
Retired Moderator
23
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 18th May 2003 18:53
GOTO... [shudders]

Do you want Windows menus in your DBP apps? - Get my plugin: http://snow.prohosting.com/~clone99/downloads/tpc_menus_102.zip
Gamer
23
Years of Service
User Offline
Joined: 29th Apr 2003
Location:
Posted: 18th May 2003 23:06
I agree with Rob K...I don't use either...DBPro doesn't truly support the use of subroutines, does it? To me, GOSUB is really GOTO since both jump to a label except GOSUB can return; there's no parameter passing allowed.

Rob K
Retired Moderator
23
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 19th May 2003 01:10
For some REALLY strange reason, DBS were encouraging us to use functions instead of GOTOs / GOSUBs when DBP was released, but all the example code uses GOSUBs.

Code split up into functions is FAR easier to read and understand, since it is easy to know what variables have which scope, and follow the flow of execution of your program. The only reason I use labels is so that I can jump to places quickly in the editor, using the Project panel.

Do you want Windows menus in your DBP apps? - Get my plugin: http://snow.prohosting.com/~clone99/downloads/tpc_menus_102.zip
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th May 2003 01:19
Programming is all about looping. You do everything until something tells it to stop.

Even your main code can be a loop. The trigger to stop is the fact that it has happened once.

If you code like this, it's easy to scale (expand) your code later. For example, you may run your program once at the moment, but later, you might add a menu at the front, and want to do "it" every time someone clicks the option.

Thanks in advance.
All the Best,
StevieVee
andrew11
23
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 19th May 2003 06:54 Edited at: 19th May 2003 06:59
I use alot of both...

Gosubs are better for long code that uses many variables, or does something like load objects, settings, etc.

Functions, on the other hand, are better for shorter code that will be called many times and uses few variables.

So, In my opinion, a little of each is best. Just be sure to keep your functions at the BOTTOM of your code, or any code underneath it will not be run.

I wish functions and subroutines in DBPro were on seperate pages like in QBASIC. Then it would be really organized.

"Food is güd!" -Dean
"All programmers are playwrites and all computers are lousy actors" -Anon
Click Here!!!
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th May 2003 11:01
Quote: "I wish functions and subroutines in DBPro were on seperate pages like in QBASIC. Then it would be really organized"


Use #include and put them in separate files.

Declare your #includes at the top of your main program.

Thanks in advance.
All the Best,
StevieVee
andrew11
23
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 19th May 2003 22:39 Edited at: 19th May 2003 22:41
I know that. I mean that the editor should display them on seperate pages, but be in one file. Have you ever used QBASIC?

"Food is güd!" -Dean
"All programmers are playwrites and all computers are lousy actors" -Anon
Click Here!!!
Darkheart
23
Years of Service
User Offline
Joined: 2nd May 2003
Location:
Posted: 20th May 2003 18:14
While Goto's should not be overused as they _can_ make code more difficult to follow sometimes it is just a f*** of a lot easier to goto rather than gosub/function etc.

My rule is to always attempt to program in a nicely structured way but not to make programming in a structured way an obsticle to achieving your goal. In the long run very probably no-one will know or care if you stuck a few gotos into your code because it was faster/easier for you.

Whatever works.

Darkheart

Login to post a reply

Server time is: 2026-07-11 04:23:22
Your offset time is: 2026-07-11 04:23:22