Gosub, Gosub -11, Purpose – Rockwell Automation 1746-BAS BASIC LANGUAGE User Manual

Page 107: Syntax, Examples, Simple subroutine, Nested subroutine

Advertising
background image

Publication 1746-RM001A-US-P

Execution Control and Interrupt Support Functions 8-11

GOSUB

Purpose

Use the GOSUB statement to cause the module to transfer control of the program
to the line number [ln num] following the GOSUB statement. In addition, the
GOSUB statement saves the location of the statement following GOSUB on the
control stack so that you can perform a RETURN statement to return control to
the statement following the most recently executed GOSUB statement. You may
nest the GOSUB statement up to 9 times.

The control stack (C-stack) stores all information associated with loop control
(example: DO-WHILE, DO-UNTIL, FOR-NEXT and BASIC subroutines). The
control stack is 157 bytes long. DO-WHILE and DO-UNTIL loops and GOSUB
commands use 3 bytes of the control stack. FOR-NEXT loops use 17 bytes.

Syntax

GOSUB [ln num]

Examples

Simple Subroutine

READY

>1

REM EXAMPLE PROGRAM

>10

FOR I = 1 TO 5

>20

GOSUB 100

>30

NEXT I

>40

END

>100 PRINT I

>110 RETURN

READY

>RUN

1

2

3

4

5

READY

>NEW

Nested Subroutine

>1

REM EXAMPLE PROGRAM

>10

FOR I = 1 TO 3

IMPORTANT

Excessive nesting exceeds the limits of the control stack,
generating an error, and causing the module to enter Command
mode.

Advertising