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 / Nesting Subroutines

Author
Message
ArcherSam
15
Years of Service
User Offline
Joined: 13th Mar 2011
Location: Florida, US
Posted: 27th Jun 2011 09:19
Hey, I recently had an experience where a nested GOSUB command was returning back to a previous GOSUB command when RETURN was called. Is it better to use GOTO or GOSUB command's when nesting these commands in a subroutine. Also awkardly I was able to use the RETURN command with the GOTO command when the help file states that this is not allowed. Weird!

Example with GOTO


Or

Example with GOSUB

Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 27th Jun 2011 10:27
Well it really depends on your needs but most if not all will say that it is far better to use gosub than goto. I myself have never found a reason to use goto and I think that gosub has a far more structured approach to programming than goto. So my recommendation is to use gosub instead of goto.

Grog will probably post and give info on why not to use goto

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 27th Jun 2011 11:06
@ Hodgey:

Yeah, I'll give him the short version.


@ ArcherSam:

GOTO bad!




No, really you probably weren't following the program flow right. When you use GOSUB it keeps track of each jump in a stack so it RETURNs to the right places in your code. If it sees a RETURN without a GOSUB (like trying to use GOTO with RETURN) it'll silently crash with no error to show you. It's always best to keep away from GOTO because any project you make with it will be hard to follow if it gets large. GOTO forces you to use another label and another GOTO to return to for example the next line after the first GOTO. GOSUB makes it much easier with RETURN that automatically goes to the line after the GOSUB. If you need further convincing click my signature to learn about the dreaded Spaghetti Code.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 27th Jun 2011 11:10
that article makes me hungry


Why does blue text appear every time you are near?
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Jun 2011 16:44
Quote: "Also awkardly I was able to use the RETURN command with the GOTO"


Quote: "Example with GOTO"


Er? Where's the RETURN?
ArcherSam
15
Years of Service
User Offline
Joined: 13th Mar 2011
Location: Florida, US
Posted: 27th Jun 2011 17:05 Edited at: 27th Jun 2011 17:24
Thanks Grog and Hodgey, was a little unsure which command to use. The awkward problem with the GOTO ended when I rebooted my computer(DarkBASIC and Windows 7 don't get along ), but I still have a weird problem while using GOSUB; it repeats Boot_Graphic subroutine!

Main.dba


Graphic.dba


My output is:
1
2
3
1
2
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 27th Jun 2011 18:19
It may be because when Darkbasic compiles the program all separate code files are combined to one single file so to the computer it sees the code like the following.



Because in the normal program flow there is nothing stopping the program (like a WAIT KEY or END) before the Boot_Graphic label it does it again... but when it sees the RETURN (without the GOSUB) it should crash silently. How you're able to actually see the screen before this program ends is a mystery.

For your main program use this instead to stop the flow from continuing on to Boot_Graphic:


Of course it's much easier with small programs to just use one file rather than multiple files.

Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 27th Jun 2011 19:34
Use functions. It's safer than horrid GoTo's and less horrid but still pretty bad Gosubs.



My signature is NOT a moderator plaything! Stop changing it!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Jun 2011 01:22 Edited at: 28th Jun 2011 01:40
Have you got an end statement at the end of main.dba (before any subroutines and functions)? If not then perhaps execution is continuing into the subroutine code?

Edit Or perhaps an earlier subroutine without a return?
ArcherSam
15
Years of Service
User Offline
Joined: 13th Mar 2011
Location: Florida, US
Posted: 28th Jun 2011 02:58
Thanks Grog and Gandalf, putting the END statement at the end of my code fixed the problem. I just hope I don't run into any weird problems anytime soon.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 28th Jun 2011 08:48
@ Neuro Fuzzy (sorry I meant to reply to you earlier):

Hahaha I generally don't like spaghetti, or lasagna but the text about ravioli code makes me drool.


@ ArcherSam:

Np... for now while you're learning Darkbasic Pro keep away from using multiple files for your code and you'll be fine.

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 28th Jun 2011 12:52
I've seen horrible people (not mentioning any names ) use gosubs in horrible ways... Here's a shorter example of a code piece I had to fix for a project he was making:



It ran fine if you looked at it from the outside, but he never actually calls the return command after going into a subroutine... That is NOT the way you use gosubs, the stack pointer keeps piling up until it overflows

TheComet

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Jun 2011 13:03
Are recursive function calls any different? In either case you need to be careful with the program's logic.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 28th Jun 2011 18:44 Edited at: 28th Jun 2011 18:44
@ TheComet:

Wow, that's crazy. In that case it would be better for him to use GOTO since it wouldn't overflow.

GregA
15
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 28th Jun 2011 18:53 Edited at: 28th Jun 2011 18:55
It would probably be ~more correct~ to do something like this...

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 28th Jun 2011 19:27
Yeah, the old 'gosub loop' can be a pretty easy mistake to make for new comers. Could potentially be hard to track, if it's only missing a return every refresh say, as the app might run for a long time before it dies.

ie.



GregA
15
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 28th Jun 2011 20:42
Well for a newcomer I would resist the urge to abstract the thing out even much. IMHO.

would be better to do



Once again this is imho... But noobs tend to abstract things to much, especially in a very high level language like db...

Login to post a reply

Server time is: 2026-07-10 22:37:12
Your offset time is: 2026-07-10 22:37:12