What is the difference between BASIC GOTO and GOSUB

What is the difference between GOTO and GOSUB operations in the BASIC programming language?

+8
source share
3 answers

GOTO just jumps to another line, GOSUB keeps track of where it came from (apparently on the stack), so when the interpreter encounters RETURN , it returns to the last place, GOSUB was called by GOSUB .

+16
source

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

GOSUB does almost the same as GOTO, but he remembers where he was. When you use the RETURN operator, it just bounces back without going to the top of the stack and flipping over again, so it is much faster. If you want your code to work fast, you should put your most-called routines at the top of the stack and use GOSUB / RETURN instead of GOTO.

+3
source

When GOTO is called, the program will jump to the corresponding line and continue execution.

If you use GOSUB, it will do the same, however at some point you can encode the RETURN instruction and the code will return to the line immediately after GOSUB.

So, GOTO goes to X, and GOSUB goes to X, but I remember where you are now, and so you can return later.

+1
source

All Articles