GOSUB [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation
HP Business BASIC/XL Reference Manual
GOSUB
The GOSUB statement unconditionally transfers control to a specified
line. The line must be in the same program unit as the GOSUB statement.
If that line is not executable (for example, if it is a comment or
declaration), control transfers to the first executable statement
following it. GOSUB can be entered as GO SUB, but HP Business BASIC/XL
will always list it as GOSUB.
GOSUB routines can be nested. It is however, a good programming practice
to treat these routines as local subunits, that is always using the GOSUB
routine to execute them and the RETURN statement to return from them. It
is possible to use the GOTO statement into and out of subroutines, but
this not a good structured programming practice and should be avoided.
The number of levels of nesting is limited. It is set with the COPTION
MAXGOSUB. The default for this COPTION is 10.
The RETURN statement returns control to the line following the GOSUB
statement (see the RETURN statement in this chapter).
Syntax
{GOSUB }
{GO SUB} line_id
Parameters
line_id Line number or line label of the line that control
transfers to. It must be in the same program unit as
the GOSUB statement.
Examples
10 REM Main Program Unit
20 GOSUB 90 !Transfer to line 90
30 REM Print sides of square
40 PRINT "| |"
50 PRINT "| |"
70 GO SUB 90 !Is listed as GOSUB 90
80 STOP
90 REM Subroutine to print top and bottom of the square
100 PRINT "+----+"
120 RETURN
999 END
When the above program is listed, line 70 will list as GOSUB 90.
The program prints this square:
+----+
| |
| |
+----+
The GOSUB statement is a local subroutine call. The local subroutine is
not a separate program unit (subunit); it belongs to the program unit
that contains it. Parameters cannot be passed to it, but it can access
all variables declared in that program unit.
10 REM Main Program Unit
20 A = 3
30 GOSUB 100
40 PRINT A
50 STOP
100 REM Subroutine
110 A = 1
120 RETURN
999 END
In the above program, line 40 prints 1 (not 3).
MPE/iX 5.0 Documentation