GOSUB[edit | edit source]
Syntax[edit | edit source]
Description[edit | edit source]
Executes the subroutine located at the indicated line number or statement label. The subroutine should include a
RETURN causes execution to continue at the statement following the
GOSUB is an outdated method for executing routines; it's generally a better idea to encapsulate your routines in a
LOCAL FN function. However, there are a couple of possible advantages to using
- Routines called using
GOSUBmay execute somewhat faster than local functions.
- You can create a "private" subroutine inside a local function, and use a
GOSUBwithin that local function to call the subroutine. The variables used in the subroutine will have the same scope as the local function. This may be a good way to execute certain repetitive tasks within the local function.
Example:Subroutines can be executed in a "nested" fashion; i.e., one subroutine may call another. FB keeps track of where each
RETURN statement should "return" to.
PRINT "First line."
PRINT "Fifth line."
PRINT "Second line."
PRINT "Fourth line."
PRINT "Third line."
It is possible for a
GOSUB statement inside a local function to jump to a subroutine located outside of that function; it's also possible for a
GOSUB statement in "main" to jump to a subroutine located inside a local function. However, this kind of programming is not recommended, because the resulting switches in variable scope can produce unexpected results.
See Also[edit | edit source]
RETURN FN; LOCAL FN