Author Topic: Sub VS Gosub/Return  (Read 3090 times)

0 Members and 1 Guest are viewing this topic.

Offline RobinGravel

  • Newbie
  • Posts: 30
    • View Profile
Sub VS Gosub/Return
« on: February 10, 2022, 10:38:44 pm »
I think gosub/return and sub() are the same.

Are there some situations it would prefer using sub() instead gosub/return?

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Sub VS Gosub/Return
« Reply #1 on: February 10, 2022, 11:28:18 pm »
That's an excellent question.

Just off the top of my head, quick and dirty:

In a situation in which one would want to execute a subroutine with a set of values, and then again with a different set of values, one would likely want to use Sub.

Say I want to call the subroutine with the variable X, and then again with the variable Y.  I could instead just use Gosub/return with variable Z, setting Z = X when I want to deal with X, then setting Z = Y when I want to deal with Y, but I find that clunky.

If the subroutine always refers to the values currently stored in the same variables every time, then Gosub/Return is A-1.

Another scenario:  Without knowing the internals of QB64, I'm guessing that if the subroutine needs to define some variables just for the subroutine's purpose, better to go with Sub so that those internal variables and associated memory get relinquished when the subroutine is done.  Using Gosub/return, whatever variables needed just for the Gosub/return processing are global?


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Sub VS Gosub/Return
« Reply #2 on: February 10, 2022, 11:36:19 pm »
SUB is a stand-alone SUBMODULE.  GOSUB is a GOTO SUBROUTINE.  Though similar, they're quite different.

First big difference:  SUB has localized variables.  GOSUB's variables are of the same scope as the module they're located in.

Quick example:

X = 3
Gosub Foo 'Goto the Subroutine
Call Foo2 'Call the SUB

Foo:
    PRINT X
RETURN


SUB Foo2
   PRINT X
END SUB

With the gosub, the variable X is in the same module (the main module), so it prints 3.  With the SUB, x is localized within the SUBMODULE Foo2, and has no value.  It prints 0.



Due to this localized modulation, SUB makes for better "plug-and-play" code than GOSUB does -- thus the reason almost all libraries will use SUB instead of GOSUB.  There's simply less chance of variable corruption with localized variables, and memory usage is smaller as those local variables free themselves when the module they're in closes.

Take this sub for an example:

SUB I13
   I = 13
END SUB

Now, you rip that out of your existing program and add it to anything you like without issues, like so:

FOR I = 1 TO 10
    PRINT I
    I13
NEXT

In the above, I13 won't affect the value of I in that FOR... NEXT loop.

If it was a GOSUB, however, it would -- ending execution of that loop after a single pass.




In my personal opinion, SUB is a much better tool for use than GOSUB.  It's just soooo much more portable and self-contained.  99.9987% of the time, I'd choose a SUB over a GOSUB.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Sub VS Gosub/Return
« Reply #3 on: February 10, 2022, 11:42:07 pm »
Using Gosub/return, whatever variables needed just for the Gosub/return processing are global?

They're of the same scope as the module that the GOSUB is located within.

For example:

SUB Foo
    FOR x = 1 TO 10
          GOSUB foo2
    NEXT

    EXIT SUB

    foo2:
         PRINT x
    RETURN
END SUB

Here we have the SUB foo and the GOSUB foo2.  X isn't global -- it's localized to the module that called it (Foo).  When we exit Foo, x is freed from memory.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RobinGravel

  • Newbie
  • Posts: 30
    • View Profile
Re: Sub VS Gosub/Return
« Reply #4 on: February 11, 2022, 11:59:39 am »
Thanks guys for the help.

Now I understand better.