What are the difference between a GOTO and a GOSUB statements in BASIC programming language?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With