Wenfang Du
Wenfang Du

Reputation: 11397

Is the `return` statement necessary at the end of an AHK script?

My one-line script:

Shift::Send ^{Space}

Is it necessary to add a return statement like the following?

Shift::Send ^{Space}

return

Upvotes: 0

Views: 1107

Answers (2)

Wenfang Du
Wenfang Du

Reputation: 11397

Quote an example from the return documentation to demonstrate what it does:

The first Return separates the hotkey from the subroutine below. If it were not present, pressing the hotkey would cause Sleep 1000 to be executed twice.

#z::
MsgBox The Win-Z hotkey was pressed.
Gosub MySubroutine
return

MySubroutine:
Sleep 1000
return

Also, quote from Hotkeys Introduction and Simple Examples:

If a hotkey needs to execute only a single line, that line can be listed to the right of the double-colon. In other words, the return is implicit:

#n::Run Notepad

As a side note, if binding multiple hotkeys to the same action, we can write:

#a::
#b::MsgBox

Upvotes: 0

William
William

Reputation: 131

In the case of a subroutine with a label like you show above, the subroutine will be called and will continue until it gets to either return or exit.

So in this example, return isn't necessary

With functions, return is actually assumed in ahk so you don't necessarily need to include it unless your passing it with an expression.

In either case though, you might want to include it just to make things more readable.

Upvotes: 1

Related Questions