In the AHK2 documentation at https://www.autohotkey.com/docs/v2/Functions.htm#Variadic
...at the end of the first section it says "To permit a function to accept any number of parameters without creating an array to store the surplus parameters, write * as the final parameter (without a parameter name)."
I scoured the web and this forum for more information, tried a couple of different things in test scripts to see if I should interpret that as being about calling a function (it is right after a sentence about calling non-variadic functions,) or a function definition, and couldn't make anything work either way. At least with the function definition (i.e. testFunc(a, b, *){ ; etc... }) the script would run, if nothing else.
I even asked Bing CoPilot, which was incapable of responding without using a parameter name. Is anyone willing to explain?
Variadic function with * as the final parameter (without a parameter name) Topic is solved
Re: Variadic function with * as the final parameter (without a parameter name)
Hello,
A parameter name is provided in the definition of a function.
A parameter name is provided in the definition of a function.
Code: Select all
#Requires AutoHotkey v2.0
f(1, 2, 3, 4)
f(a, *) {
MsgBox a
}
- FanaticGuru
- Posts: 1907
- Joined: 30 Sep 2013, 22:25
Re: Variadic function with * as the final parameter (without a parameter name) Topic is solved
Maybe an example will help:
FG
Code: Select all
testFunc(1,2,3,4,5,6)
testFunc(a, b, *) ; all parameters passed after the first two are discarded
{
MsgBox a ' ' b
}
testFunc2(1,2,3,4,5,6)
testFunc2(a, b, c*) ; all parameters passed after the first two are stored in the array c
{
MsgBox a ' ' b
for stuff in c
MsgBox stuff
}
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: Variadic function with * as the final parameter (without a parameter name)
Thanks folks,
I knew it had to be something simple, but getting caught up in the first sentence of the documentation paragraph I quoted, and thinking it was a function call thing, rather than definition, and the description of variadic function calls and __Enum and all that...
Also, my stupid test probably would have showed the answer, I think I just stopped too soon because I was looking for the extra parameters in the locals with the debugger's object browser
Anyway, thanks again!
I knew it had to be something simple, but getting caught up in the first sentence of the documentation paragraph I quoted, and thinking it was a function call thing, rather than definition, and the description of variadic function calls and __Enum and all that...
Also, my stupid test probably would have showed the answer, I think I just stopped too soon because I was looking for the extra parameters in the locals with the debugger's object browser
Anyway, thanks again!
Re: Variadic function with * as the final parameter (without a parameter name)
Actually, I just realized I responded to quick, mixing FanaticGuru's examples together in my head sorta, thinking the first would result in repeated MsgBoxes for each pair of called args, which would at least make sense.
But the first example's behavior was exactly what I'd observed in testing. I suppose the purpose for this functionality (heh) is to prevent improper counting of args from stopping a script
But the first example's behavior was exactly what I'd observed in testing. I suppose the purpose for this functionality (heh) is to prevent improper counting of args from stopping a script
Re: Variadic function with * as the final parameter (without a parameter name)
@FanaticGuru ah nice I didn't think of it as "discarded" but nice way to put it.
Re: Variadic function with * as the final parameter (without a parameter name)
Since I'm just learning v2 of AHK (from being a long-term v1 user) I'm gradually learning the new stuff. Just came across a documented reason to have the final solo * arg in a function/method definition. It's pretty self-explanatory...
https://www.autohotkey.com/docs/v2/lib/OnExit.htm
PS. When I first ran across the statement I mentioned in my original post, I was sure it meant there was a way to get your params* without having a params[] array. I am a weird thinker, so I may be the only person ever to make that assumption
https://www.autohotkey.com/docs/v2/lib/OnExit.htm
PS. When I first ran across the statement I mentioned in my original post, I was sure it meant there was a way to get your params* without having a params[] array. I am a weird thinker, so I may be the only person ever to make that assumption
Who is online
Users browsing this forum: aleape, RussF, User-AutoHotkey and 32 guests