Difference between commands and functions (if there is one)

Chris, a Client of ours, was working through our Intermediate AHKV2 Course had a very interesting question:
In the Send Video, Isaias is describing Send as a command, but shouldn’t he be calling it a function, or a function call statement? I thought commands were version1 only. Please clarify.
So lets take a moment and think about the answer to that question.
He is correct: everything is a “Function” in AHK v2 but you could definitely say the same about AHK v1… everything is a function under the hood.
Usually we think about Commands and Functions as being different entities but they are in fact the same thing.
They have just a tiny little difference. So let me explain the difference between a command call and a function call:
A function optionally takes parameters, performs a series of actions and RETURNS a value.
A command optionally takes parameters, performs a series of actions, but it doesn’t RETURN values.
e.g.:
pos := RegexMatch(str, ‘test’) ; <— this function matches the string and returns the position where it was matched
Send ‘^a^c{tab}^v{enter}’; <— this command performs a series of actions (sends keystrokes) but doesn’t return anything
Interestingly enough there is something that you have to keep in mind:
While the parenthesis are optional for commands, they are not optional if you need to capture a value from a function.
e.g.:
RegexMatch str, ‘test’; <— in this example you performed the match but you have no way of knowing the position
pos := RegexMatch str, ‘test’ ; <— this will throw an ERROR.
For all of those reasons you can say that Send even though it is a function under the hood, you always use it as a command and that’s why I called it the Send command.
You can definitely do this if you want:
Send(‘^a^c{tab}^v{enter}’) ; <— you can use parentheses on a command call
But hey, why would you want to type more?! haha
As developers, we are constantly typing, so naturally we try to type the least we can…
So I use command calls when I don’t need values and function calls when I care about the result returned by the action.
In Summary: Functions can behave like commands, but commands can never behave like functions.
This is merely a semantics issue and it doesn’t really affect how your script works.
If you dive deeper on that topic you will find that there are more lower level differences like:
Commands could modify the environment and have “artifacts” (like creating a file, think of FileAppend, FileCopy, FileRecycle) while Functions are supposed to be pure, they never affect anything other than the parameters that where given and they return a modified copy of what you gave them (like Sort(), StrUpper(), FormatTime()).
But again, those are differences that you don’t need to worry about when you are creating simple scripts.

If you want to learn more about Functions, Objects or Classes, we recommend you taking a look at our Intro to Functions, Classes & COM.

 

Comments are closed.