Gosub Command
Obsolete
The Gosub command is obsolete and was used in procedural code prior to object-oriented DataFlex versions (OOP DataFlex was released in version 3.0 in 1991). You should use Classes and Objects that use Procedures and Functions instead of subroutines.
Purpose
To move execution to a part of a program (a subroutine) located under a label and then return execution to the next line after the gosub.
Syntax
gosub label
Argument Explanation
label must be given literally in the command, without a colon or quotation marks.
What It Does
The Gosub command "stacks" the number of the statement that follows the gosub command line and transfers execution to the part of the program (a subroutine) at label. When a Gosub_Return command is executed in the subroutine, the number is taken off the stack and execution continues at the line following the gosub, unless the Gosub_Return command named a label itself, in which case execution goes there.
showln 1
gosub demo
showln 3
abort
demo:
showln 2
Gosub_Return
This program first displays 1. Then, the gosub command line passes execution to the line labelled demo:. From there, the line displaying 2 is executed, and then the Gosub_Return. The Gosub_Return passes execution back to the third line from the beginning, which displays 3. After that, the program terminates.
Labels are any unbroken unique string at the beginning of a command line ending in a colon (:).
Notes
-
DataFlex can "stack" up to 18 line numbers at one time, adding a number each time a
gosubis executed, and removing the most-recently added number each time aGosub_Returnis executed. All subroutines must end in a return command. -
Even if there are no errors at compile time, if your program allows multiple executions of
gosubwithout aGosub_Return, the maximum "stack depth" may be exceeded at run time, in which case Error 97 (Multiple GOSUBS without RETURN) will be triggered. -
Gosubcommand lines may be controlled with conditionals, such as indicators or if tests. -
The
gotocommand transfers execution to a label, but does not stack the return address as doesgosub. You cannot use theGosub_Returncommand to return to the command line following agotocommand. -
Labelled subroutines may be located in the program's main path of execution (above the final
abort,system, orchaincommand), but it is preferable to locate them below the terminating command to ensure that they are executed only when intended (bygosubcommands).