GO SUB
GO SUB
Section titled “GO SUB”Syntax
Section titled “Syntax”GO SUB <label>GOSUB <label>
Continues the execution at the given label or line number. The current execution point is pushed (stored) onto the stack, to be recovered later.
When a RETURN is found and executed, the previous execution point is popped out (recovered) from the stack and continues just after the GO SUB. This is a way to create simple subroutines.
This sentence exists just for compatibility with legacy BASIC dialects. You should use SUB or FUNCTION instead.
GO SUB cannot be used within neither subroutines nor functions. You can’t GOSUB into a function or sub. So GOSUB is limited to global scope
WARNING: Using GO SUB continuously without returning with RETURN will eventually fill the stack (stack overflow) and crash your program.
Example with GO SUB
Section titled “Example with GO SUB”10 LET number = 1020 GOSUB 1000 : REM calls the subroutine30 LET number = 2040 GOSUB 1000 : REM calls the subroutine again100 END : REM the program must end here to avoid entering the subroutine without using GOSUB1000 REM Subroutine that prints number + 11010 PRINT "number + 1 is "; number + 11020 RETURN : REM return to the caller
This will output:
number + 1 is 11number + 1 is 21
Remarks
Section titled “Remarks”- This statement is Sinclair BASIC compatible.
- GO SUB cannot be used within subrutines nor functions.