Author Topic: GOSUB VS SUB  (Read 12040 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
GOSUB VS SUB
« on: April 16, 2019, 07:56:02 pm »
When should one use a (GOSUB + RETURN) vs using (SUB + END SUB)?

FellippeHeitor

  • Guest
Re: GOSUB VS SUB
« Reply #1 on: April 16, 2019, 08:30:54 pm »
Both serve the same purpose but SUB is more versatile.

FellippeHeitor

  • Guest
Re: GOSUB VS SUB
« Reply #2 on: April 16, 2019, 08:31:35 pm »
And easier to maintain. But there's no rule and both are valid strategies for code reusing.

FellippeHeitor

  • Guest
Re: GOSUB VS SUB
« Reply #3 on: April 16, 2019, 08:32:32 pm »
In summary, it's up to you.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: GOSUB VS SUB
« Reply #4 on: April 16, 2019, 08:54:37 pm »
You should use SUB ... END SUB always for
1. recursive subs
2. variable isolation from main code and other subs
3. passing arguments

Marked as best answer by Raptor88 on April 16, 2019, 06:10:58 pm

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: GOSUB VS SUB
« Reply #5 on: April 16, 2019, 09:09:27 pm »
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: GOSUB VS SUB
« Reply #6 on: April 16, 2019, 10:01:58 pm »
In summary, it's up to you.
Thanks for your input.
Raptor88

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: GOSUB VS SUB
« Reply #7 on: April 16, 2019, 10:10:33 pm »
You should use SUB ... END SUB always for
1. recursive subs
2. variable isolation from main code and other subs
3. passing arguments
1. recursive subs
.... beyond my scope for now, but good to know.

2. variable isolation from main code and other subs
.... This I don't understand.  When I pass a variable as an argument, changes made to the variable within the sub changes the original variable.  I read that this is by design.

3. passing arguments
.... Yes, I haven't seen how to pass arguments using GOSUB, so SUB would be the way to do that.  Good point.

Thanks for the help,
Raptor88

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: GOSUB VS SUB
« Reply #8 on: April 16, 2019, 10:13:35 pm »
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.

Thank you for the history lesson.  I didn't know any of what you just explained.  I will use SUB + END SUB as you recommend since that is the more modern method.
Raptor88

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: GOSUB VS SUB
« Reply #9 on: April 16, 2019, 11:24:40 pm »
Quote
2. variable isolation from main code and other subs
.... This I don't understand.  When I pass a variable as an argument, changes made to the variable within the sub changes the original variable.  I read that this is by design.

Yes this is true about the variable arguments passed to the sub but all the variables created inside the sub are erased when the sub finishes so if you use an i in the sub, it won't effect the i in the main code as it would with GOSUB.


Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: GOSUB VS SUB
« Reply #10 on: April 17, 2019, 12:27:36 am »
Yes this is true about the variable arguments passed to the sub but all the variables created inside the sub are erased when the sub finishes so if you use an i in the sub, it won't effect the i in the main code as it would with GOSUB.

Ahhh, now the light bulb has turned on.  Thanks for taking the time to explain this valuable info.
Raptor88

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: GOSUB VS SUB
« Reply #11 on: April 17, 2019, 08:05:18 am »
Hi Raptor88,

Here is an example of a recursive sub. It calls itself over and over again until the job is done.

IMHO it is the most beautiful and elegant kind of code you can write. I hope this fractal inspires:
Code: QB64: [Select]
  1. _TITLE "recurring squares 2017-10-26 by bplus"
  2. ' Now with Alpha coloring!
  3. 'reoccuring squares SmallBASIC translation from
  4. REM reoccuring squares NaaLaa started 2015-05-14 MGA/B+
  5.  
  6. CONST xmax = 700
  7. CONST ymax = 700
  8.  
  9. SCREEN _NEWIMAGE(xmax, ymax, 32)
  10. _SCREENMOVE 360, 30 'adjust as needed _MIDDLE needs a delay .5 or more for me
  11. COMMON SHARED dimmer
  12. sq = 700: dir = 1
  13.     CLS
  14.     white& = _RGB(255, 255, 255)
  15.     fRecStep 0, 0, sq, sq, white&
  16.     sqPlus sq / 2, sq / 2, sq / 2
  17.     _DISPLAY
  18.     _LIMIT 30
  19.     dimmer = dimmer + dir
  20.     IF dimmer > 255 THEN dimmer = 255: dir = dir * -1: _DELAY .5
  21.     IF dimmer < 0 THEN dimmer = 0: dir = dir * -1: _DELAY .5
  22.  
  23. SUB fRecStep (x1, y1, x2, y2, c&)
  24.     LINE (x1, y1)-STEP(x2, y2), c&, BF
  25.  
  26. SUB sqPlus (x, y, side)
  27.     cx = x - side / 2: cy = y - side / 2
  28.     fRecStep cx, cy, side, side, _RGBA(0, 0, 0, dimmer)
  29.     IF side < 10 THEN EXIT SUB
  30.     ns = side / 2: nc = colorNumber - 35
  31.     sqPlus cx, cy, ns
  32.     sqPlus cx + side, cy, ns
  33.     sqPlus cx, cy + side, ns
  34.     sqPlus cx + side, cy + side, ns
  35.  
  36.  

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: GOSUB VS SUB
« Reply #12 on: April 17, 2019, 12:09:32 pm »
To further add to what I said:

In early versions of BASIC there was no variable scope.  All variables were global.  Line numbers were required (10, 20, 30, 40, etc.) and GOSUB jumped to a line number.  QBASIC got rid of the requirement for line numbers, and added optional line labels (labels behave like line numbers, in global scope, but have an easier to remember name rather than a number).  I can still remember the first time I used QBASIC after years of Commodore 64 BASIC and DOS GW-BASIC, it was so weird not needing line numbers!  I could barely believe it.  And an IDE that showed you your whole program!  In GW-BASIC you were left going LIST to display your lines of code, and if it was more than a screen of lines, you'd be forced to work out a range to display, like LIST 150-180.

Code: QB64: [Select]
  1. 10 CLS
  2. 20 INPUT "Enter number 1: ", Num1!
  3. 30 INPUT "Enter number 2: ", Num2!
  4. 30 PRINT Num1!; "+"; Num2!; "="; (Num1! + Num2!)
  5. 40 GOTO 20

People usually went by 10s for line numbers, that way you could add up to 9 more lines in-between existing lines without being forced to renumber existing lines to make room.  For example, in the above addition app, if I wanted to print a blank line after the answer, I could do that as:

Code: QB64: [Select]

QBASIC with no line number requirements was a breath of fresh air!  It made coding MUCH simpler and easier, especially when making changes then testing, changes then testing... in GW-BASIC you had to go by 100s if you wanted to make sure you would have enough space to add groups of lines later, or else forced to use GOTO spaghetti code to jump around, which was an absolute nightmare for code readibility!

GOSUB came from that time period.  It was better than GOTO, because you returned to where you started after, but it was still confusing keeping track of all the subroutines splashed around the code.  I would often add huge line numbers for my subs so they stayed at the bottom, such as starting my subs at line 8000.

SUB + END SUB was an amazing game changer when it came out.  No strict line numbers, neatly grouped together at the end of your code, accessible within the IDE with F2 including pressing F2 on a subroutine's name, new FUNCTION ability with a data type (e.g. MyStringFunc$ returns a string).

The only time to use GOSUB in a modern BASIC would be quick-and-dirty proof-of-concept programming.  Hacking something together where the only goal is getting it done fast.  For the most manageable code that is easiest to read and quick to update, definitely stick to SUB + END SUB / FUNCTION + END FUNCTION.  In my opinion.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: GOSUB VS SUB
« Reply #13 on: April 17, 2019, 12:51:54 pm »
The only time to use GOSUB in a modern BASIC would be quick-and-dirty proof-of-concept programming.

I wouldn't even bother using it then.  Just pretend that GOSUB has never existed.

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: GOSUB VS SUB
« Reply #14 on: April 17, 2019, 01:11:11 pm »
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.

I think you're right about F2.  I run a lot of legacy code, using GOSUB, and only recently created a new program with a SUB/ END SUB construction, because it was necessary to pass a variable.

The SUB/END SUB showed up in edit mode, and I couldn't remember how to get back to see the main program listing.  What are you supposed to do?

Malcolm

(PS: I was running it in QB45, not QB64, on this occasion, as it was being run on a Linux platform.)
« Last Edit: April 18, 2019, 06:47:29 am by MWheatley »