Specifications
Commands - 49
GOSUB
Statement
SYNTAX: GOSUB line/label
.
.
.
RETURN
PURPOSE: To branch to and return from a subroutine.
REMARKS: line/label is the beginning of the subroutine.
A subroutine may be called any number of times in a program, and a subroutine may be called from
within another subroutine.
The RETURN statement causes CAMBASIC to branch back to the statement following the most
recent GOSUB statement. A subroutine may contain more than one RETURN statement, should logic
dictate a return at different points in the subroutine. Subroutines may appear anywhere in the
program.
To prevent inadvertent entry into the subroutine, you may put an END or GOTO statement in front of
it to direct program control around the subroutine.
NOTE: The execution of the GOSUB statement is independent of the location of the target line in the
program. No run–time search occurs.
When a label is used with GOSUB, a statement cannot follow GOSUB on the same line.
EXAMPLE: 10 GOSUB 40
20 PRINT "Back from subroutine"
30 END
40 PRINT "subroutine";
50 PRINT "in";
60 PRINT "progress"
70 RETURN
RUN
subroutine in progress
Back from subroutine
The following shows the use of labels:
90 A3 = AIN(0)
100 GOSUB ..FILTER
110 PR FL
.
.
.
3000 ..FILTER
3010 FL = .875 * FL + .125 * A3
3020 RETURN
ERROR: <Can’t compile> – if line/label does not exist