Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the difference between a BASIC GOTO and GOSUB statement

Tags:

basic

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

like image 927
Morufu Salawu Avatar asked Oct 10 '12 01:10

Morufu Salawu


2 Answers

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.

like image 187
Collin Avatar answered Sep 19 '22 23:09

Collin


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.

like image 40
Greyphilosophy Avatar answered Sep 18 '22 23:09

Greyphilosophy