AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Variable number of function arguments

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4016
Location: Pittsburgh

PostPosted: Sat Apr 09, 2005 2:46 am    Post subject: Variable number of function arguments Reply with quote

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
}
...
Back to top
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3004
Location: Minnesota

PostPosted: Sat Apr 09, 2005 2:57 am    Post subject: Reply with quote

Oh, I'm experimenting all right. *evil snicker*
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Sat Apr 09, 2005 3:51 am    Post subject: Re: Variable number of function arguments Reply with quote

Laszlo wrote:
We can even have variable number of arguments with a little trick, as the example below shows.
Nice trick and a great set of functions you put together. Although I'm going to look into native support for a varying number of arguments, support for default parameters -- which allow the caller to omit optionally omit them -- is probably a higher priority.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group