Action Script provides a mechanism to separate logical chunks of a script into subroutines. The gosub statement is used in a script to execute a subroutine placed in the code.
Subroutines are defined by a label in the script. The “main:” label is a special label since it does not define a subroutine but the initial code to be executed for the script. Note that all labels are case-insensitive.
The following example shows a script that starts at Main:, and then follows an execution path via gosub statements and defined subroutines:
main:
gosub A
gosub C
exit
A:
print("A")
gosub B
return
B:
@script::log("B")
return
C:
@script::log("C")
return
The output of the example script would be:
A
B
C
Note that the exit and return statements are optional, as they are inferred if they are missing. The following script is functionally equivalent to the previous example:
main:
gosub A
gosub C
A:
@script::log("A")
gosub B
B:
@script::log("B")
C:
@script::log("C")
A subroutine or the main: will never execute another subroutine unless it is specifically referenced in a gosub statement.