IB Statements/gosub
From CometWiki
GOSUB statement
Syntax: GOSUB statement-label
Discussion: The GOSUB statement directs program control to the specified statement-label and places the next executable address in a subroutine stack.
Subsequent execution of a RETURN statement will direct program control to the saved address (the next statement following the original GOSUB statement).
The statement-label defines the beginning of a "subroutine," even though the program lines are contained in the main Internet Basic program itself. Subroutines themselves may invoke other subroutines -- these are called "nested subroutines." Subroutines may be nested up to 16 levels. Attempting to nest beyond this depth will result in a "GOSUB STACK OVERFLOW" exception.
Also see POP and POPALL.
Example 1:
OPEN (1) "C1A" EXCP=9000 READ (1,1000) EXCP=8000 PRINT (0) "PROCESSING CUSTOMER:";CUSTNO$ GOSUB 1000 PRINT (0) "COMPLETED CUSTOMER:";CUSTNO$ . STOP ! Main routine 1000 CRAVAIL = CRLIMIT - ARTOTAL . . . RETURN ! Subroutine
In this example, the main section of the program opens a data file called "C1A", then reads a record from the file (in sequential order). Next, the program displays a message on the video terminal (logical unit 0) and branches to a subroutine at statement-label 1000.
Processing continues in the subroutine until the program encounters the RETURN statement. At this point, control is transferred back to the main section of the program (to the statement immediately after the GOSUB 1000 statement).
The STOP statement is included in the main section of the program to prevent program flow from entering the subroutine without using the GOSUB statement.
Example 2:
GOSUB ONE . . . RUN "QMONITOR" ! ONE: ! Subroutine 1 . GOSUB TWO . RETURN ! TWO: ! Subroutine 2 . GOSUB THREE . RETURN ! THREE: ! Subroutine 3 . . . RETURN
This example shows the skeleton construction of nested subroutines. The main section of the above program includes a statement to direct control to a subroutine labelled ONE.
In subroutine ONE, there is a call to a nested subroutine called TWO. From this subroutine, there is a further call to a nested subroutine called THREE.
Notice that all of the subroutines end with a RETURN statement, which directs control to the statement following the most recent GOSUB call.