Gosub

AutoHotkey

Gosub

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

Gosub, Label

Parameters

Label

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

Remarks

As with the parameters of almost all other commands, Label can be a variable reference such as %MyLabel%, in which case the name stored in the variable is used as the target. However, performance is slightly reduced because the target label must be "looked up" each time rather than only once when the script is first loaded.

When using a dynamic label such as %MyLabel%, 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