11

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

Morufu Salawu
  • 175
  • 1
  • 1
  • 8

3 Answers3

24

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.

Collin
  • 11,977
  • 2
  • 46
  • 60
  • Basic is still used. VB.NET is still BASIC. – Preet Sangha Oct 10 '12 at 01:35
  • 2
    What? VB.NET is an entirely different language. Sure, it's called Basic, but it's not `BASIC` – Collin Oct 10 '12 at 01:37
  • @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. – Preet Sangha Oct 10 '12 at 01:41
  • 5
    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. – Glenn Sallis Oct 11 '12 at 15:02
  • @GlennSallis Is that a product? The words seem to be too generic to google effectively. – Collin Oct 11 '12 at 15:06
  • 4
    @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. – Glenn Sallis Oct 11 '12 at 15:19
  • Pretty sure you were quoted verbatim in the series [Halt and Catch Fire](http://www.imdb.com/title/tt2543312/) – a paid nerd May 31 '15 at 03:39
5

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 instruction set 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 instruction set 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 instruction set 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.

Greyphilosophy
  • 149
  • 2
  • 3
  • 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. – BlackJack Jun 25 '22 at 11:01
  • @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. – Greyphilosophy Jun 26 '22 at 15:55
1

When you call GOTO the program will jump to the statement 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 statement 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.

bg117
  • 354
  • 4
  • 13
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 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 Oct 10 '12 at 19:48
  • 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 – Morufu Salawu Oct 10 '12 at 22:13
  • @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. – Preet Sangha Jun 26 '22 at 22:33
  • 1
    @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. – BlackJack Jun 27 '22 at 07:00
  • Ahh, excellent point @BlackJack. Thank you for the correction. Please feel free to edit the answer to make it more correct with your example. – Preet Sangha Jun 28 '22 at 08:29