What are the difference between a BASIC GOTO and GOSUB statement

11,484

Solution 1

GOTO simply jumps to another line, GOSUB keeps track of where it came from (on a stack, presumably), so when the interpreter encounters a RETURN, it goes back to the last place GOSUB was called.

Solution 2

The other answers provided give a good explanation on how to use GOTO and GOSUB, but there is an important difference in how they are processed. When a GOTO is executed it starts at the top of the stack and flips through all the lines of code until it finds the line it is supposed to GOTO. Then if you use another GOTO statement to get back, it again goes to the top of the stack and flips through everything until it gets to the next location.

GOSUB does almost the same thing as GOTO, but it remembers where it was. When you use the RETURN statement it just jumps back without first going to the top of the stack and flipping through everything again, so it's much faster. If you want your code to run fast you should put your most called subroutines at the top of the stack and use GOSUB/RETURN instead of GOTO.

Solution 3

When you call GOTO the program will jump to the line in question and carry on executing.

If you use GOSUB, it will do the same, however at some point you can code a RETURN statement and the code will return to the line just after the GOSUB.

So GOTO is go to X while GOSUB is go to X but remember where you are now and so you can return later.

Share:
11,484
Morufu Salawu
Author by

Morufu Salawu

Updated on June 05, 2022

Comments

  • Morufu Salawu
    Morufu Salawu almost 2 years

    What are the difference between a GOTO and a GOSUB statements in BASIC programming language?

  • Preet Sangha
    Preet Sangha over 11 years
    Basic is still used. VB.NET is still BASIC.
  • Collin
    Collin over 11 years
    What? VB.NET is an entirely different language. Sure, it's called Basic, but it's not BASIC
  • Preet Sangha
    Preet Sangha over 11 years
    @Colin - you're missing the point. VB.NET is still BASIC. Yes it has many OO extensions but it's still in essence BASIC. Sure it's not interpreted but that's an execution strategy not a language concern.
  • Morufu Salawu
    Morufu Salawu over 11 years
    Thank you; however, I think we can incorporate another transfer of commands where the intended executable instruction ended back to the next line that follows the GOTO statement, then another transfer of control statement above the the line of the first control transfer to do the same, though more codes than just ordinary GOSUB then single RETURN. eg 10 GOTO 30. 15 Codes. 20 Codes. 25 GOTO 45. 30 Codes. 35 Codes. 40 GOTO 15. 45 END this will work and it will never goes into an unending loop.
  • Morufu Salawu
    Morufu Salawu over 11 years
    Your answer is correct! I am only trying to put another method that will involve more codes. Thank you for the question answered. I already voted for your answer
  • Glenn Sallis
    Glenn Sallis over 11 years
    BASIC is driving many high performance business systems in Logistics, banking and many other industries. All MultiValue database driven business systems for example, are written in BASIC.
  • Collin
    Collin over 11 years
    @GlennSallis Is that a product? The words seem to be too generic to google effectively.
  • Glenn Sallis
    Glenn Sallis over 11 years
    @Collin True, very generic indeed. "MultiValue Databases" are indeed products. There are a few on the market worth mentioning such as UniVerse and UniData from Rocket Software, D3 from Tiger Logic and Reality from Northgate Information Solutions. They are both DBMS and "business" engine packed into one with BASIC as the built-in language of choice for the business logic.
  • a paid nerd
    a paid nerd almost 9 years
    Pretty sure you were quoted verbatim in the series Halt and Catch Fire
  • BlackJack
    BlackJack almost 2 years
    The use of „stack“ is quite confusing here as it is technically wrong as the lines are not forming a stack, opposed to GOSUBs internal use of a stack data structure. Also the description of GOTO uses just one possible implementation. Not all go through the program from the first line. Some look at the current line number and start there if the GOTO's target is equal or higher.
  • Greyphilosophy
    Greyphilosophy almost 2 years
    @BlackJack, in the old days there was a physical stack of punch cards. Hench why I also use the term "flip" to describe moving through it. I could see how an implementation could optimize GOTO. However, there is still an inherent limitation with interpreted languages like BASIC where you can't just point at a memory reference and go; the interpreter has to find where it is going first before it can continue executing the program. The exception being the RETURN from GOSUB.
  • Preet Sangha
    Preet Sangha almost 2 years
    @BlackJack - techincally both can be correct. If your BASIC is using lines (such as Acornsoft BASIC), each statement appears on its own numbered line, and there are no blank lines. That is true even in the line contains a REM statement.
  • BlackJack
    BlackJack almost 2 years
    @PreetSangha that's not my point. Consider this line: 10 GOSUB 42:PRINT"X" — a RETURN will not return to the next line after 10 but to the next statement after GOSUB. In this case the PRINT statement. Of course the next statement could be in the next line, but the answer sounds like it always continues with the next line.
  • Preet Sangha
    Preet Sangha almost 2 years
    Ahh, excellent point @BlackJack. Thank you for the correction. Please feel free to edit the answer to make it more correct with your example.