Author Topic: Practical Difference Between Using GOSUB/GOTO / label / END SUB vs EXIT SUB?  (Read 6801 times)

0 Members and 1 Guest are viewing this topic.

Offline davidshq

  • Newbie
  • Posts: 55
    • View Profile
I have some code (simplified) that looks like this:

SUB dosomething
  IF x = 2 THEN
    y = 10
    GOSUB ending
  END IF
  x = 4
ending:
END SUB

Is there any reason not to change this to:

SUB dosomething
  IF x = 2 THEN
    y = 10
    EXIT SUB
  END IF
  x = 4
END SUB

I know in some circumstances GOSUB is used to run some portion of code and then return back to where it was called, but in this case there is no return nor even any other code that could be executed, the sole purpose of the GOSUB/GOTO is to reach END SUB.

Thanks!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
You wouldn't want a GOSUB there, as eventually you'd run out of stack space.  You're going somewhere, but you're never coming back from it, and eventually the PC is going to crap out of stack space listing all those never return addresses and your program is going to crash.

GOTO would work perfectly fine for you, as it doesn't place any return address on the stack, but use of it will make some of the newer programmers wrinkle up their nose, spritz holy water all around, and force them to offer up 100 Hail Mary's to atone for the blasphemy they perceive....

EXIT SUB is simple, to the point, and it's easy to understand its purpose just by reading it, without requiring any scrolling or jumping to a label to see what it's intending to do.  I'd suggest just using it and save yourself the headache from having to listen to the overly religious Antigotoers out there.  You just can't go wrong with a simple, self-documenting command like EXIT SUB.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline davidshq

  • Newbie
  • Posts: 55
    • View Profile
Thanks McNeill!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Quote
GOTO would work perfectly fine for you, as it doesn't place any return address on the stack, but use of it will make some of the newer programmers wrinkle up their nose, spritz holy water all around, and force them to offer up 100 Hail Mary's to atone for the blasphemy they perceive....

LOL

Why not
Code: QB64: [Select]
  1. SUB dosomething
  2.       IF x = 2 THEN y = 4 ELSE x = 4 'and be done with it.

no cursing GOTO's or even EXIT SUB's

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
@bplus
LOL
Quote
Why not
Code: QB64: [Select]
SUB dosomething
      IF x = 2 THEN y = 4 ELSE x = 4 'and be done with it.
END SUB

no cursing GOTO's or even EXIT SUB's
simply because it is a challenge to write code without ELSE keyword!

If I must write a FOR loop from 1 to 10 without FOR keyword I should write
Code: QB64: [Select]
  1. DIM a AS INTEGER, StringOne AS STRING
  2. a = 0
  3. Repeat:
  4. a = a +1
  5. StringOne = StringOne + STR$(a)
  6. PRINT a, StringOne
  7. IF a < 10 GOTO Repeat
  8. PRINT "Done"
  9.  

GOTO is anywhere!
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
LOL

Why not
Code: QB64: [Select]
  1. SUB dosomething
  2.       IF x = 2 THEN y = 4 ELSE x = 4 'and be done with it.

no cursing GOTO's or even EXIT SUB's

Because: "I have some code (simplified) that looks like this:"

His code is just simplified down to the absolute smallest point to highlight his question/need.  It's not his actual program code at all.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Quote
simply because it is a challenge to write code without ELSE keyword!

Sorry, I thought the challenge was to be Practical! ;-))

BTW goto END like this:
Code: QB64: [Select]
  1. SUB dosomething
  2.   IF x = 2 THEN
  3.     y = 10
  4.     GOTO 311
  5.   END IF
  6.   x = 4
  7. 311 END SUB
  8.  
  9.  

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
GOTO would work perfectly fine for you, as it doesn't place any return address on the stack, but use of it will make some of the newer programmers wrinkle up their nose, spritz holy water all around, and force them to offer up 100 Hail Mary's to atone for the blasphemy they perceive....

Actually, I will now have to crucify my computer, incinerate it, then take the ash and vaporize in a 10 megaton thermonuclear blast! THEN take those atoms and introduce them to anti-atoms and annihilate whats left.  THANKS!


In all seriousness, there really is no reason to have GOTOs, GOSUBs or EXITs anymore, all code should be written to run in a seamless line of execution. And it can be done.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Actually, I will now have to crucify my computer, incinerate it, then take the ash and vaporize in a 10 megaton thermonuclear blast! THEN take those atoms and introduce them to anti-atoms and annihilate whats left.  THANKS!


In all seriousness, there really is no reason to have GOTOs, GOSUBs or EXITs anymore, all code should be written to run in a seamless line of execution. And it can be done.

As usual I disagree on this specially EXIT and the perfectly valid reason being 10 more lines of code or 10 more minutes spent or 10 more people confused by the work around to avoid using these.

What, for instance, is your workaround for the above demo problem for which apparently you aren't allowed to use ELSE either ;-))

BTW
Code: QB64: [Select]
  1. dosomething:
  2. IF x = 2 THEN y = 10: RETURN
  3. x = 4
« Last Edit: June 27, 2020, 06:40:58 pm by bplus »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
I've found that since I've largely excised GOTO, GOSUB & labels (except for locating DATA fields), I've had a much easier time of reacquiring a grasp of what my older coding does. Before that I would open an old project, full of spaghetti logic and find myself wondering what the hell it was I was trying to do. Linear procedural coding is much easier to follow. Plus the IDE makes it much easier to jump to a SUB rather then do a search for a label.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Yeah back in the day, you could write really compact code with GOTO and GOSUB but reading it later say to fix a bug or make a mod was nightmare. Back then space was so much more valuable and sometimes less space meant less time to run.

Offline davidshq

  • Newbie
  • Posts: 55
    • View Profile
Thanks for the feedback all.

I'm working with an existing code base that uses a mixture of labels, gotos, gosubs, exit subs, etc. I'm slowly refactoring it so that it is more modular and easier for me to understand. So for now, I'll be going with EXIT SUB but may refactor more in the future...and may find some instances in which goto/gosub is still the best option...but I have done a decent amount of OOP, so I have the inclination (whether healthy or otherwise) to lean away from goto/gosub/line labels.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
It doesn't really matter whether you use GOTO or EXIT SUB. I'll use GOTO particularly if there's multiple points I can have an early return at and there's some cleanup code I need to run before exiting:
Code: [Select]
sub s
If x then goto fail
...
If y then goto fail
...
Fail:
Close #f
End sub

The important thing is that you don't use GOSUB because calling GOSUB without RETURN will exhaust the stack.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
What, for instance, is your workaround for the above demo problem for which apparently you aren't allowed to use ELSE either ;-))

I am guessing your referencing this,
LOLsimply because it is a challenge to write code without ELSE keyword!

I thought he was simply stating that it was more challenging to not use ELSE.

Now I didn't say one of the Ten Commandments was "Thou Shalt Not Use Goto, Gosub, or Exit"
I just stated that there is really no reason too, back in BASICA and GWBASIC or other early forms you really could not code with out them. But with the QBs and QB64 their are simply better ways to achieve the work.

Though all this is actually mute really cause once QB64 converts to the CPP the output is full of GOTOs!

as for a work around for this:
Code: QB64: [Select]
  1.     SUB dosomething
  2.       IF x = 2 THEN
  3.         y = 10
  4.         GOTO 311
  5.       END IF
  6.       x = 4
  7.    311 END SUB
Here: XD
Code: QB64: [Select]
  1.     SUB dosomething
  2.       IF x = 2 THEN
  3.         y = 10
  4.       END IF
  5.       x = 4
  6.     END SUB
okay, lets have a FOR to leave early after finding 2 and use the x = 4 line as some kind of error checking,:
Code: QB64: [Select]
  1.   SUB dosomething
  2.    FOR x = 0 TO 10
  3.      IF x = 2 THEN
  4.        y = 10
  5.        x = 12
  6.      END IF
  7.    NEXT x
  8.    IF x = 11 THEN  x = 4 'x was never matched
  9.   END SUB
     

Back to my real work for the day, Cloning "Phantasy Star" from the Sega Master System now.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Quote
Code: QB64: [Select]
  SUB dosomething
   FOR x = 0 TO 10
     IF x = 2 THEN
       y = 10
       x = 12
     END IF
   NEXT x
   IF x = 11 THEN  x = 4 'x was never matched
  END SUB
     

Back to my real work for the day, Cloning "Phantasy Star" from the Sega Master System now.

That's not quite it, x should come out either 2 if came in 2, changing y to 10, or come out 4 leaving y unchanged, but you proved my point, thankyou!  ;-))

Good luck with the real work for the day!