Previous  |  Next ]     [ Up  |  First  |  Last ]     (Article 226 of 271)
 
 
GOSUB and RETURN
 
A GOSUB transfers program execution to the block number specified in the GOSUB statement.
Execution will continue until a block containing a RETURN statement is encountered. The
RETURN will transfer control back to the block immediately following the GOSUB statement.
To use a GOSUB statement, the called block number must be part of the same program.
Generally the subroutines are at the end of the main program.
 
The GOSUB format is as follows.
 
GOSUB  ####  L###
  Line #  Loop Count
    (optional)
If the L is omitted the GOSUB routine will be executed once.
N1 
N2 
N3
N4 GOSUB 100  Main Program 
N5
.
.
.
N90 M30 
N100 
N101
.
.  Subroutine
N200
N201 RETURN 
N202
 
When the GOSUB is executed in N4 the program will jump to N100 and start executing until
N201 is reached. At N201 control will transfer to N5 and lines N5 thru N90 will be executed.
The M30 will terminate the main program and keep lines 100 thru 202 from being executed
again. Note: subroutine nesting is allowed to 50 levels deep.