When you omit a parameter in a built-in command (not function), what you're really doing is passing an empty string. There are differences between these two cases:
- Defining a function with an optional parameter followed by mandatory parameters.
- Calling a command with an omitted optional parameter followed by optional parameters which have not been omitted.
With both MsgBox and MouseGetPos, all parameters are optional.
AutoHotkey 1.1 allows the following with user-defined functions:
Foobar(1,, 3)
Foobar(baz, blah="something", blivet="")
{
MsgBox baz=%baz%, blah=%blah%, blivet=%blivet%
}
This is only possible when the parameter's default value is known (i.e. not when calling a function dynamically).
Allowing the middle parameter to be omitted
If you do not want to change the order of the parameters or make two of the three optional, you can do a bit of "juggling":
Foobar("baz", "blivet")
Foobar("baz", "blah", "blivet")
Foobar(baz, p2, p3="omitted")
{
blah := p3="omitted" ? "default" : p2 ; optional
blivet := p3="omitted" ? p2 : p3 ; required
MsgBox baz=%baz%, blah=%blah%, blivet=%blivet%
}
This way, the function always requires at least two parameters, and you're effectively allowed to omit the parameter in the middle when calling the function. However, you need to reserve a (string or numeric) value to indicate the parameter has been omitted. This can be avoided with AutoHotkey 1.1 by using a variadic function:
Foobar("baz", "blivet")
Foobar("baz", "blah", "blivet")
Foobar(baz, p2, p3*)
{
blah := p3.MaxIndex() ? p2 : "default" ; optional
blivet := p3.MaxIndex() ? p3[1] : p2 ; required
MsgBox baz=%baz%, blah=%blah%, blivet=%blivet%
}
Alternatively, the function can be declared as Foobar(baz, p*)
and you can base your conditionals on p.MaxIndex()
(the number of additional parameters), but in that case, only the first parameter is mandatory.