beautypg.com

Basic programming guide – Remote Processing BASIC for the CX-10 Modbus User Manual

Page 45

background image

BASIC PROGRAMMING GUIDE

2-36

ON GOSUB

Syntax:

ON expr GOSUB line0[,line1[,line2...]]
Where: expr = 0 to number of subroutines after GOSUB

linen = subroutine line number to execute

Function:

Calls subroutine based on value of expr.

Mode:

Run

Use:

ON A GOSUB 100, 200, 500

DESCRIPTION

The ON-GOSUB instruction conditionally branches to one of several possible subroutines depending on the
value of expr. expr must evaluate to greater than or equal to zero. If expr evaluates to zero, execution
branches to line0. When expr evaluates to one, execution branches to line1, etc. If necessary, expr is truncated
to an integer.

ON-GOSUB saves the location of the program statement after ON-GOSUB on the control stack and
immediately transfers program control to the selected subroutine. The subroutine is then executed. When
Basic encounters the RETURN instruction, program execution resumes at the program statement after ON-
GOSUB. ON-GOSUB instructions can be nested.

One or more of linen may be the same, to execute the same subroutine with different expr values. At least
one linen must be provided. linen can be in any order.

RELATED

ON GOTO, GOSUB, RETURN

ERRORS

BAD ARGUMENT The value of expr is less than 0
BAD SYNTAX

The expr value is larger than the number of subroutine locations provided, or commas
were omitted between {subr n line#} values, or no subroutine locations were given.

C-STACK

Attempted recursion caused control stack overflow

EXAMPLE

10

P=2

20

ON P GOSUB 1000, 3000, 2000

30

END

1000 PRINT "Line 1000"
1010 RETURN
2000 PRINT "Line 2000"
2010 RETURN
3000 PRINT "Line 3000:
3010 RETURN

>run
Line 3000