AutoHotkey Variadic Functions can be incredibly handy when you don’t know the exact number of parameters. The below example shows some simple math where you can pass any number of parameters to be added. It also demonstrates how you can use MaxIndex() to determine how many parameters were passed to the function.
MsgBox % math(1,2)
MsgBox % math(1,2,3)
MsgBox % math(1,2,3,4)
for k, y in X
tot:=y + tot
loop, % x.MaxIndex() ;Loop over the # of parameters that exist
Indiv.= x[A_Index] "+"
return SubStr(Indiv,1,(StrLen(Indiv)-1)) "=" tot ;use substr to remove the last + sign
Demonstration using AutoHotkey Variadic Functions
In my initial video I talked through how & why to use AutoHotkey functions as well as how to return one value from a function. This video will demonstrate how to Return Multiple Values from AutoHotkey Functions via the byRef command. You can also return multiple values by returning an Object or an Array which can hold any-number of values.. The byRef command can seem a bit tricky at first, but it really is an easy way to return many values from a function. My next video will cover using the Include command
Return multiple values from AutoHotkey functions with byRef
AutoHotkey functions are a true gem that help streamline your code, make it more readable, easier to maintain, and avoid conflicts with other variables (because their scope is local by default. They take a little practice getting used to however they can save a ton of time and allow for re-using a ton of code. Don’t get hung-up on writing them at the beginning of your script. Often I go back after writing my script and realize I should have written a function in place of a subroutine.
My first video on AutoHotkey functions explains why & how you can use them.