On GoSub

From BR Wiki
Jump to navigation Jump to search

The On Gosub (ON GOS) statement evaluates a numeric expression, then transfers program control to a subroutine according to the expression's value.

Comments and Examples

GOSUB matches the rounded integer value of an expression to its corresponding line-ref. If the value is 3.25, for example, program control transfers to the subroutine that begins at the line indicated in the third line-ref.

In the following example, control goes to line 640 when T is 0, to line 740 when T is 4, and to line 840 when T is 6. When T/X + 1 rounds to a number less than one or greater than three, control goes to line 90.

00850 X=3
00860 ON T/X + 1 GOSUB 640,740,840 NONE 90


ON <numeric expression> GOSUB <line ref>[,...] [NONE <line ref>] [<error condition> <line ref>][,...]


1.) Transfer control to the next statement if there is no corresponding line reference.
2.) Interrupt the program if an error occurs while ON is not active.


ON GOSUB has two required parameters. "Numeric-expression" is the numeric expression -a constant, variable, relational expression, or function -that the system is to evaluate.

"Line-ref" is the name or number of the line to which control transfers when the value of the numeric expression is matched to the position order of the line-ref.

The optional "NONE line-ref" parameter allows you to specify the line that control should go to when the value of the numeric expression does not fall into the indicated range. This branch is treated as a GOSUB branch rather than an error-processing branch. The branched-to subroutine must end with a RETURN statement.

ON GOSUB also provides error processing with the optional "error-cond line-ref" parameter. See Error Conditions for more information.

Technical Considerations

1.) Return information for the ON GOSUB statement is kept in the FLOWSTACK. See the FLOWSTACK specification in the BRConfig.sys chapter for more information.