beautypg.com

Rpbasic-52 programming guide, Gosub – Remote Processing BASIC 52 User Manual

Page 63

background image

RPBASIC-52 PROGRAMMING GUIDE

2-44

GOSUB

Syntax:

G O S U B line number
...
line number program state ments
R E T U R N

Function:

Transfers program control to the specified line number. The RETURN causes execution to resume at
t h e p r og r am s t at e m e nt a ft e r G O S U B .

Mode:

Run

Use:

1 0 0 FO R A = 1 to 2 0 : G O S U B 2 0 0 : N E X T A : E N D
2 0 0 PR I N T A , S Q R ( A ) : R E T U R N

Cards:

All

D E S C R IP T I ON

GOSUB provides subroutine capability within RPBASIC-52 programs. A subroutine may be called from
within another subroutine.

GOS UB sav es the location of the program state ment after G OSU B on the C -Stack and im mediately transfers
program control to line number. When a RETURN is encountered, program execution resumes at program
s t at e m e nt a ft e r G O S U B .

GOSUB s can be nested. The number nesting is limited by available C-Stack RAM, but is usually enough for
at least 30 routines.

RELATED

G O T O , O N -G O T O , O N -G O S U B

E R R O R

C - S T A C K An unexpected RETURN is encountered or the number of subroutines executed was excessive.

EXAMPLE

10 GOSUB 100
20 PRINT "Back from routine"
30 END
100 PRINT "In subroutine"
110 RETURN

>run

In subroutine
Back from routine