Futurebasic/Language/Reference/gosub

GOSUBEdit

SyntaxEdit

GOSUB {lineNumber|"statementLabel"

DescriptionEdit

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. FB 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: 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 AlsoEdit

RETURN FN; LOCAL FN

Last modified on 7 July 2009, at 19:51