Gosub – Rockwell Automation 1771-DB BASIC MODULE User Manual

Page 165

Advertising
background image

Chapter
Statements

11

11 -13

Use the GOSUB statement to cause the BASIC module to transfer control
of the program to the line number the GOSUB statement references.
In addition, the GOSUB statement saves the location of the next statement
after the GOSUB on the C-stack, so that you can perform a RETURN
statement (page 11 -34) to return control to that statement after the
GOSUB executes. You may nest the GOSUB statement.

The control stack (C-stack) stores all information associated with loop
control. The C-stack is 157 bytes long. GOSUB statements use 3 bytes of
the C-stack. Do not improperly exit this subroutine or a C-stack error may
occur. See CLEARS (page 11 -3).

Important: Excessive nesting exceeds the limits of the C-stack, generating
an error, and causing the module to enter Command mode.
For more information see control stack, page 8 -1.

Important: Always use a RETURN (page 11 -34) to exit a GOSUB.
C-stack errors may occur if you do not use a RETURN.

Syntax

GOSUB

ln num

Example

Simple Subroutine

Nested Subroutine

>10 FOR I = 1 TO 3

>10 FOR I = 1 TO 3

>20 GOSUB 100

>20 GOSUB 100

>30 NEXT I

>30 NEXT I

>40 END

>40 END

>100 PRINT I

>100 REM USER SUBROUTINE HERE

>110 RETURN

>105 PRINT I,

READY

>110 GOSUB 200

>RUN

>120 RETURN

1

>200 REM START OF NESTED SUBROUTINE

2

>210 PRINT I*I

3

>220 RETURN

READY

>RUN

1 1

2 4

3 9

GOSUB

Advertising