10.6.1 GOSUB and RETURN Statements
The GOSUB statement unconditionally transfers control to
a line in a subroutine. The last statement in a subroutine
is a RETURN statement, which returns control to the first
statement after the calling GOSUB. A subroutine can con-
tain more than one RETURN statement so you can return
control conditionally, depending on a specified condition.
The following example first assigns a value of 5 to the vari-
able A , then transfers control to the subroutine labeled Times_
two . This subroutine replaces the value of A with A multi-
plied by 2. The subroutine's RETURN statement transfers
control to the first PRINT statement, which displays the
changed value. The program calls the subroutine two more
times, with different values for A . Each time, the RETURN
transfers control to the statement immediately following the
corresponding GOSUB.
EXAMPLE: Click to display example.
Output
10
30
50
Note that BASIC signals ``RETURN without GOSUB'' if
it encounters a RETURN statement without first having
encountered a GOSUB or ON GOSUB statement.