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

Gosub, 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.


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.

Return, Functions, IsLabel(), Blocks, Loop, Goto, A_ThisLabel


This example is executed as follows:

  1. Jump to the label named "Label1".
  2. Show the message "The Label1 subroutine is now running."
  3. Return to the line immediately after Gosub.
  4. Show the message "The Label1 subroutine has returned (it is finished)."
  5. End the auto-execute section.
Gosub, Label1 
MsgBox, The Label1 subroutine has returned (it is finished).

MsgBox, The Label1 subroutine is now running.