GO SUB

BASin

GO SUB GO to SUBroutine

Statement/Command

GO SUB causes the program to branch to a subroutine, which is a seperate section of the program. This is useful if a subroutine is required several times in a program.

How to use GO SUB

GO SUB may be used as a statement or direct command and it is followed by a numeric value, for example

GO SUB 1000

On execution, the value following GO SUB (1000 above) is rounded to the nearest integer, and the program branches to the line number having this value. The use of a variable or expression enables the program to branch to a subroutine at a calculated line number. Note that if the line number does not exist, the program still branches and continues with the first statement that is then encountered.

A subroutine ends with RETURN, and the program then branches back to the statement following the GO SUB statement. Subroutines may be nested so that one is reached from another, in which case the RETURN sends the program back to the statement following the last GO SUB statement executed.

The GO SUB stack

Whenever GO SUB is executed, its line number is placed on the GO SUB stack in memory. If two or more GO SUBs are executed before RETURN, their line numbers stack up so that the last number is on top of the stack. RETURN always takes the top line number from the stack and goes to this line to continue the program.

Note that error 4 (Out of memory) can occur if there are not enough RETURN statements.

Format

  • GO SUB int-num-expr

See also

Chapter 5.