Rpbasic-52 programming guide, On gosub – Remote Processing BASIC 52 User Manual
Page 89

RPBASIC-52 PROGRAMMING GUIDE
2-70
ON GOSUB
Syntax:
O N expr G O S U B l in e 0 [, li n e1 [ ,l in e 2 .. .] ]
Where: expr = 0 to n u m be r of s ub r ou t in e s af t er G O S U B
linen = subroutine line number to e xecute
Function:
Calls subroutine based on valu e of expr.
Mode:
Run
Use:
ON A G OSUB 100, 200, 500
Cards:
All
D E S C R IP T I ON
The ON-GO SUB instruction conditionally branches to one of several possible subroutines depending on the
value of expr. expr must evalu ate to greater than o r equal to zero. If expr evaluates to zero, execution
branches to line0. When expr evaluates to one, execution branches to line1, etc. If ne cessa ry, expr is truncated
to an integer.
ON-GOS UB saves the location of the program statement after ON-GOSU B on the control stack and
immediately transfers program control to the selected subroutine. The subroutine is then executed. When
B a s ic e n co u n te r s t h e R E T U R N i ns t ru c ti o n, p ro g ra m e x ec u ti o n r e su m e s at t he p ro g ra m s ta t em e n t af t er O N -
GOSUB . ON-GOSUB instructions can be nested.
One or m ore of linen may be the same, to ex ecute the sam e subroutine w ith different expr values. At least
one linen mu st be pro vided . linen can be in an y order.
RELATED
O N G O T O , G O SU B , R E T U R N
ERRORS
B A D A R G U M E N T The value of expr is less than 0
B A D S Y N T A X
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 - S T A C K
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