AutoHotkey Use functions instead of labels General example of functions vs labels


Example

I'll demonstrate the basic usage of using functions vs using labels+gosub.
In this example we'll implement simple functionality to add two numbers and store them in a variable.

With functions:

c := Add(3, 2) ; function call
MsgBox, Result: %c%

Add(a, b) { ; This is a function. Put it wherever you want, it doesn't matter.
    ; the a and b inside of this function are set by the function call above
    Return a+b ; the function will return the result of the expression "a+b"
}

With labels (please don't do that):

a := 3 
b := 2 
GoSub, Add ; execute the label "Add" then jump back to the next line here
MsgBox, Result: %c%
Return ; without this, the label would be executed again for no reason.

Add: ; This is a label. Please put them at the bottom of your script and use "Return" in a line above.
    c := a+b
Return