GoSub...Return Statement
GoSub transfers program control to a subroutine. Once the subroutine is complete, program control returns back to the line following the GoSub instruction which initiated the subroutine.
Syntax
GoSub {label}
{label:}
statements
Return
Parameters
- label
- When the user specifies a label, the program execution will jump to the line on which this label resides. The label can be up to 32 characters in length. However, the first character must be an alphabet character (not numeric).
Description
The GoSub instruction causes program control to branch to the user specified statement label. The program then executes the statement on that line and continues execution through subsequent line numbers until a Return instruction is encountered. The Return instruction then causes program control to transfer back to the line which immediately follows the line which initiated the GoSub in the first place. (i.e. the GoSub instruction causes the execution of a subroutine and then execution returns to the statement following the GoSub instruction.) Be sure to always end each subroutine with Return. Doing so directs program execution to return to the line following the GoSub instruction.
Potential Errors
Branching to Non-Existent Statement
If the GoSub instruction attempts to branch control to a non-existent label then an Error 3108 will be issued.
Return Found Without GoSub
A Return instruction is used to "return" from a subroutine back to the original program which issued the GoSub instruction. If a Return instruction is encountered without a GoSub having first been issued then an Error 2383 will occur. A standalone Return instruction has no meaning because the system doesn't know where to Return to.
See Also
GoTo, OnErr, Return
GoSub Statement Example
The following example shows a simple function which uses a GoSub instruction to branch to a label and execute some I/O instructions then return.
Function main
Integer var1, var2
GoSub checkio 'GoSub using Label
On 1
On 2
Exit Function
checkio: 'Subroutine starts here
var1 = In(0)
var2 = In(1)
If var1 = 1 And var2 = 1 Then
On 1
Else
Off 1
EndIf
Return 'Subroutine ends here
Fend