Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Sat Apr 09, 2005 2:46 am Post subject: Variable number of function arguments |
|
|
I could hardly wait to get home and try the new functions. Gorgeous! We can even have variable number of arguments with a little trick, as the example below shows. The MIN#(x) and MAX#(x) functions take a list of numbers, parse them into an array and search this array for the smallest or the greatest number. The calling is simple | Code: | MIN#("1, -5, 2.345, 0")
MAX#("0," x "," y "," z) | The later one uses variables. Their values are substituted at the call, the function just sees a bunch of numbers.
I copy here the test script I played with. Hope you like to experiment, too. | Code: | !1::
InputBox x, Parameter, type in a number,, 160, 140
InputBox f, Function, function name?,, 160, 140
IfEqual f, SQRT, MsgBox % SQRT(x)
IfEqual f, EXP, MsgBox % EXP(x)
IfEqual f, LOG, MsgBox % LOG(x)
IfEqual f, LN, MsgBox % LN(x)
IfEqual f, ABS, MsgBox % ABS(x)
IfEqual f, CEIL, MsgBox % CEIL(x)
IfEqual f,FLOOR, MsgBox % FLOOR(x)
return
!2::
InputBox x, Parameter 1, type in a number,, 160, 140
InputBox y, Parameter 2, type in a number,, 160, 140
InputBox f, Function, function name?,, 160, 140
IfEqual f, MIN, MsgBox % MIN(x,y)
IfEqual f, MAX, MsgBox % MAX(x,y)
IfEqual f, MOD, MsgBox % MOD(x,y)
IfEqual f, POW, MsgBox % POW(x,y)
IfEqual f,ROUND, MsgBox % ROUND(x,y)
return
!3::
InputBox x, Parameter 1, type in a number,, 160, 140
InputBox y, Parameter 2, type in a number,, 160, 140
InputBox z, Parameter 3, type in a number,, 160, 140
InputBox f, Function, function name?,, 160, 140
IfEqual f,MIN, MsgBox % MIN#(x "," y "," z)
IfEqual f,MAX, MsgBox % MAX#(x "," y "," z)
return
MAX(x,y)
{
IfLess x,%y%, Return y
Return x
}
MAX#(x) ; MAX#("1,-5,2.345," var1 "," var2)
{
StringSplit y, x, `,, %A_Space%%A_Tab%
mx := y%y0%
loop % y0-1
IfLess mx, % y%A_Index%, SetEnv mx, % y%A_Index%
Return mx
}
MIN(x,y)
{
IfLess x,%y%, Return x
Return y
}
MIN#(x) ; MIN#("1,-5,2.345," var1 "," var2)
{
StringSplit y, x, `,, %A_Space%%A_Tab%
mx := y%y0%
loop % y0-1
IfGreater mx, % y%A_Index%, SetEnv mx, % y%A_Index%
Return mx
}
MOD(x,m)
{
Transform y, mod, %x%, %m%
Return y
}
SQRT(x)
{
Transform y, sqrt, %x%
Return y
}
POW(x,p)
{
Transform y, pow, %x%, %p%
Return y
}
... |
|
|