Syntax:
gosub {lineNumber|"statementLabel"}
Description:
Executes the subroutine located at the indicated line number or statement label. The subroutine should include a return
statement; return
causes execution to continue at the statement following the gosub
statement.
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 gosub
:
- Routines called using
gosub
may execute somewhat faster than local functions.
- You can create a "private" subroutine inside a local function, and use a
gosub
within 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. FutureBasic keeps track of where each return
statement should "return" to.
print "First line."
gosub "sub1"
print "Fifth line."
end
"sub1"
print "Second line."
gosub "sub2"
print "Fourth line."
return
"sub2"
print "Third line."
return
program output:
First line.
Second line.
Third line.
Fourth line.
Fifth line.
Note:
A gosub
statement inside a local function cannot jump to a subroutine located outside of that function; similarly a gosub
statement in "main" cannot jump to a subroutine located inside a local function. gosub
programming is not recommended, a local fn
should be used as a replacement.
As of FutureBasic version 5.4.8, the new implementation of gosub/return does not support optimized compilation. If your code uses gosub/return and needs optimization, you will have to replace every subroutine by an ordinary local fn.
See Also:
return; fn <userFunction>; local fn