When you omit a parameter in an inline command (and not in a function), what you really do is pass an empty string. There are differences between the two cases:
- Defining a function with an optional parameter, followed by the required parameters.
- Invokes a command with an optional optional parameter, followed by optional parameters that were not omitted.
With MsgBox and MouseGetPos, all parameters are optional.
AutoHotkey 1.1 allows you to use custom functions:
Foobar(1,, 3) Foobar(baz, blah="something", blivet="") { MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% }
This is possible only when the default value of the parameter is known (i.e. not when the function is called dynamically).
Allow to skip the middle parameter
If you do not want to change the order of the parameters or make two of the three optional, you can "juggle" a little:
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% }
Thus, a function always requires at least two parameters, and you can really omit the parameter in the middle when the function calls . However, you need to reserve a (string or numeric) value to indicate that the parameter is omitted. This can be avoided with AutoHotkey 1.1 using the variational 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 legend on p.MaxIndex() (the number of additional parameters), but in this case only the first parameter is required.