Gosub

Gosub is used to call a subroutine at a given label. This subroutine ends with Return and shares all the local context with the calling script. There is no parameters that can be passed and no data insulation between the script that calls and the subroutine.

Syntax

 Gosub LABELGosub LABEL From SCRIPTGosub LABEL FROM =STRING_EXPR

The script can be prefixed by the folder code followed by a dot when it is only an evaluated script. If not, the script is searched in the folder. If the script is not found in the folder, then the search is carried out in the hierarchy of folders given by the adxmother variable in ascending order.

Examples

# Example 1: The final value returned here for I will be 6Subprog GET_VALUE(I)Variable Integer II=0Gosub LABEL1 : # I value is 3 after the GosubGosub LABEL2 : # I value is 5 after the GosubGosub LABEL3 : # I value is 6 after the GosubEndLABEL1 : I+=1$LABEL2 : I+=1LABEL3I+=1Return# Example 2: use computed subprogs to get execute an action : EVENT_CODE describes an event# If EVENT_CODE is equal to END, the second Gosub will call an ACTION label in a script called EVENT_END$HANDLE_EVENTGosub ASSIGN_HANDLER From ="X3.DECODING_TOOLS" : # Must be called in X3 folder even if a more local script existsGosub ACTION From =EVENT_HANDLERReturn# In DECODING_TOOL scriptLocal Char EVENT_HANDLER(40)EVENT_HANDLER="EVENT_"+EVENT_CODEReturn

Description and comments

The Gosub calls a section of code that ends with a Return.

All the context is shared, especially:
* The local variable class.
* The default class list of values.
* The tables and joins opened.
* The transaction in progress. In a script called by Gosub, you can commit a transaction that has been opened in the calling script, which is not the case if the script is called by Call, func, or fmet.
* The error handler set by Onerrgo.

Comments

No control of nesting on the Gosub / Return is done at compilation time (a return can be common to several labels). If a local label is called in a script, the existing label is verified at compilation time.

A given label must be unique in a given script (this is also checked at compilation time).

A label called by Gosub must end with a Return and not with an End.

Associated errors

Error codeDescription
31No more memory available (can happen if a recursive not ending Gosub is executed).
32Return that does not correspond to a Gosub (arrives when a Return is encountered and an End is expected).
20Script does not exist or cannot be accessed (Gosub ... From syntax), or label does not exist in the corresponding script.

See also

Goto, Return, Call, Subprog, Funprog, func, fmet.