Skip to content

GOSUB

Branch to Subroutine

This directive unconditionally branches to a statement number but remembers where it came from to allow the program to come back with a RETURN directive.

GOSUB line-ref
line-ref
is the program line number or label to branch to.

The combination of GOSUB and RETURN directives provides the ability to use subroutines in a program, which can be executed from several places within the overall program, perform a predetermined action, and return to the specific point, which referenced their execution.

Program execution resumes at the statement following the GOSUB directive when a
RETURN directive is executed.

Subroutines can be nested by placing a GOSUB directive in another subroutine, but the nested subroutines must have properly organized endings or exits to avoid errors.

To avoid possible execution errors, a subroutine should be terminated with a RETURN directive. An EXITTO directive should be used only if it is necessary to interrupt the subroutine operation and disregard the point in the program that issued the GOSUB.

This directive cannot be used in Thoroughbred Basic Console Mode.

GOSUB 08976

causes the program to start execution of the subroutine at statement number 8976; a
RETURN directive will return execution to the statement following the GOSUB directive.

IF X = 8 THEN GOSUB 8976

The placement of the GOSUB directive in an IF directive has the same effect as the first example, but is only executed if X=8

EXITTO, ON GOSUB, and RETURN directives

From the Thoroughbred Basic Language Reference, Volume II: F–Q, version 8.9.1. Open this page in the PDF