Description
Transfers program control to one of several internal subroutines that are listed as statement labels. Direction of branching is determined by computing the value of an index expression, then using that value as an index to the list of labels.
Syntax
On index GoSub label1 [ [ , labeln ] ... ]
Parameters
The On...GoSub statement has the following parameters.
Parameter | Description |
---|---|
Index | When the On...GoSub statement is encountered, the current value of index is determined, and the program transfers control to the proper subroutine. If index evaluates to 1 (one), control will branch to the first subroutine designated; if the expression evaluates to 2 (two) the program will branch to the second subroutine; and so forth. When index is evaluated it is truncated to an integer value. If the resulting value is less than 1 (one), or if the value exceeds the number of subroutine labels, an error results. |
Label | Indicates a subroutine within the program. The On...GoSub statement must be written on one line, with the labels separated by commas. Program control will return to the main program sequence when Return is encountered in the subroutine. |
On...GoSub is an efficient form of branching based on an index. It compiles as a jump table (as compared to the Case construct which offers comparative functionality, but is compiled as a series of nested If statements).
See also
GoSub, GoTo, Locate, On...GoTo, Return
Remarks
labels = "LABEL1,LABEL2,LABEL3,LABEL4" locate input_parm in labels Using "," Setting POS then On Pos GoSub LABEL1, LABEL2, LABEL3, LABEL4 end return LABEL1: * Code of some type Return LABEL2: * Code of some type Return LABEL3: * Code of some type Return LABEL4: * Code of some type Return