What are the difference between a BASIC GOTO and GOSUB statement
Asked Answered
P

3

12

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

Pastime answered 10/10, 2012 at 1:28 Comment(0)
K
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.

Kraut answered 10/10, 2012 at 1:33 Comment(7)
Basic is still used. VB.NET is still BASIC.Kilburn
What? VB.NET is an entirely different language. Sure, it's called Basic, but it's not BASICKraut
@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.Kilburn
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.Microtone
@GlennSallis Is that a product? The words seem to be too generic to google effectively.Kraut
@Kraut 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.Microtone
Pretty sure you were quoted verbatim in the series Halt and Catch FireAltonaltona
C
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.

Condone answered 9/9, 2015 at 14:53 Comment(2)
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.Tabanid
@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.Condone
K
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.

Kilburn answered 10/10, 2012 at 1:35 Comment(5)
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.Pastime
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 answerPastime
@Tabanid - 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.Kilburn
@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.Tabanid
Ahh, excellent point @BlackJack. Thank you for the correction. Please feel free to edit the answer to make it more correct with your example.Kilburn

© 2022 - 2024 — McMap. All rights reserved.