Variadic function with * as the final parameter (without a parameter name) Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Variadic function with * as the final parameter (without a parameter name)

18 Mar 2024, 18:22

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?
User avatar
mikeyww
Posts: 27178
Joined: 09 Sep 2014, 18:38

Re: Variadic function with * as the final parameter (without a parameter name)

18 Mar 2024, 19:40

Hello,

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
}
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Variadic function with * as the final parameter (without a parameter name)  Topic is solved

18 Mar 2024, 19:43

Maybe an example will help:

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
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Variadic function with * as the final parameter (without a parameter name)

18 Mar 2024, 20:00

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!
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Variadic function with * as the final parameter (without a parameter name)

18 Mar 2024, 21:07

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
jsong55
Posts: 255
Joined: 30 Mar 2021, 22:02

Re: Variadic function with * as the final parameter (without a parameter name)

18 Mar 2024, 21:48

@FanaticGuru ah nice I didn't think of it as "discarded" but nice way to put it.
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Variadic function with * as the final parameter (without a parameter name)

22 Mar 2024, 10:30

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

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: aleape, RussF, User-AutoHotkey and 32 guests