Moog Animatics SmartMotor™ Developer's Guide,Rev. L
Page 463 of 909
GOSUB(label)
Subroutine Call
APPLICATION: Program execution and flow control
DESCRIPTION: Perform subroutine beginning at C{number}
EXECUTION: Immediate
CONDITIONAL TO: C{number} previously defined
LIMITATIONS: Nesting must be <10 levels deep
READ/REPORT: N/A
WRITE: N/A
LANGUAGE ACCESS: N/A
UNITS: N/A
RANGE OF VALUES: Input: 0–999
TYPICAL VALUES: Input: 0–999
DEFAULT VALUE: N/A
FIRMWARE VERSION: 5.x and later
COMBITRONIC: GOSUB(0):3
where ":3" is the motor address — use the actual address or a
variable
DETAILED DESCRIPTION:
The GOSUB(label) command redirects program execution to a subroutine of the program
marked with a label C{number}. The end of every subroutine is marked by the RETURN
statement, which causes execution to return to the line following the corresponding GOSUB
command.
NOTE: If a GOSUB is attempted to a label that does not exist, the GOSUB is ignored
and the next program line is executed.
Subroutines may call further subroutines — that is called nesting. There may be as many as a
thousand GOSUBs, but the nesting cannot exceed nine levels deep. A counter, conditional test
or some other method can be used to stay within the nesting limit. A subroutine may call
itself, which is called recursion. However, this practice is highly discouraged because it can
lead to a stack overflow or nesting limit.
NOTE: Subroutines present a great opportunity to partition and organize your code.
The STACK control flow command explicitly and deliberately destroys the RETURN address
history. Therefore, if you issue a STACK command, ensure that the program execution does
not encounter a RETURN before the next GOSUB.
The GOSUB command is valid from both serial channels and within a user program. There are
three forms of the command that are valid:
Part 2: Commands: GOSUB(label)