Gosub

Jumps to the specified label and continues execution until Return is encountered.

Gosub Label
Gosub("Label")

Parameters

Label

The name of the label, hotkey label, or hotstring label to which to jump, which causes the functions beneath Label to be executed until a Return or Exit is encountered. "Return" causes the script to jump back to the first function beneath the Gosub and resume execution there. "Exit" terminates the current thread.

Remarks

Label can be a variable or expression only if parentheses are used. For example, Gosub MySub and Gosub("MySub") both execute the MySub: subroutine.

Performance is slightly reduced when using a dynamic label (that is, a variable or expression which returns a label name) because the target label must be "looked up" each time rather than only once when the script is first loaded. An error dialog will be displayed if the label does not exist. To avoid this, call IsLabel beforehand. For example:

if IsLabel(VarContainingLabelName)
    Gosub(VarContainingLabelName)

Although Gosub is useful for simple, general purpose subroutines, consider using functions for more complex purposes.

Related

Return, Functions, IsLabel, Blocks, Loop, Goto

Example

Gosub, Label1 
MsgBox "The Label1 subroutine has returned (it is finished)."
return

Label1:
MsgBox "The Label1 subroutine is now running."
return