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

Page 32

Advertising
background image

BASIC PROGRAMMING GUIDE

2-23

GOSUB

Syntax:

GOSUB line number
...
line number program statements
RETURN

Function:

Transfers program control to the specified line number. The RETURN causes execution to resume at
the program statement after GOSUB.

Mode:

Run

Use:

100 FOR A=1 to 20 : GOSUB 200 : NEXT A : END
200 PRINT A, SQR(A) : RETURN

DESCRIPTION

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

GOSUB saves the location of the program statement after GOSUB on the C-Stack and immediately transfers
program control to line number. When a RETURN is encountered, program execution resumes at program
statement after GOSUB.

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

RELATED

GOTO, ON-GOTO, ON-GOSUB

ERROR

C-STACK 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

Advertising