I can’t tell you the # of times AutoHotkey Variadic Functions come in handy

AutoHotkey Variadic FunctionsAutoHotkey 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.

Browser_Forward::Reload
Browser_Back::
;***********variadic function******************* 
MsgBox % math(1,2)
MsgBox % math(1,2,3)
MsgBox % math(1,2,3,4)

math(x*){
	tot:=0
	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
}

AutoHotkey Merchandise-White Stress ballDemonstration using AutoHotkey Variadic Functions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.