FutureBasic Logo

<<    Index    >> FutureBasic 5

gosub   statement

gosub {lineNumber|"statementLabel"}

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:
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."

print "Second line."
gosub "sub2"
print "Fourth line."

print "Third line."

program output:
First line.
Second line.
Third line.
Fourth line.
Fifth line.

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