Ultima2 Posted December 20, 2008 Share Posted December 20, 2008 (edited) How do I write a UDF that will take an unknown amount of parameter. Like if I want to write a function add that will add the numbers I put in the parameter. Maybe something like this? add() Func add($x, $x1, $x2... $z = 0) EndFunc My problem is that I dont know how to write the function that will accept any amount of parameters. Yes I know you can just add using + but I want to know how to write a function that will take an unknown amount of parameters like in C++. Thanks Edited December 20, 2008 by Ultima2 Link to comment Share on other sites More sharing options...
Zedna Posted December 20, 2008 Share Posted December 20, 2008 Example from standard include file: Func _ArrayCreate($v_0, $v_1 = 0, $v_2 = 0, $v_3 = 0, $v_4 = 0, $v_5 = 0, $v_6 = 0, $v_7 = 0, $v_8 = 0, $v_9 = 0, $v_10 = 0, $v_11 = 0, $v_12 = 0, $v_13 = 0, $v_14 = 0, $v_15 = 0, $v_16 = 0, $v_17 = 0, $v_18 = 0, $v_19 = 0, $v_20 = 0) Local $av_Array[21] = [$v_0, $v_1, $v_2, $v_3, $v_4, $v_5, $v_6, $v_7, $v_8, $v_9, $v_10, $v_11, $v_12, $v_13, $v_14, $v_15, $v_16, $v_17, $v_18, $v_19, $v_20] ReDim $av_Array[@NumParams] Return $av_Array EndFunc ;==>_ArrayCreate Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Ultima2 Posted December 20, 2008 Author Share Posted December 20, 2008 (edited) Can you be more clearer Zedna? I dont understand how that will help. Edited December 20, 2008 by Ultima2 Link to comment Share on other sites More sharing options...
BrettF Posted December 20, 2008 Share Posted December 20, 2008 (edited) Just pass an array as the parameter. Here is an example: Global $array[5] $array[0] = 1 $array[1] = 2 $array[2] = 3 $array[3] = 4 $array[4] = 5 $output = _Add ($array) MsgBox (0, "", $output) Func _Add ($aNumbers, $start = 0) Local $ret For $i = $start to UBound ($aNumbers) - 1 $ret += $aNumbers[$i] Next Return $ret EndFunc Cheers, Brett EDIT: I see Zedna beat me. I thought you wanted to be able to pass x numbers to a function. If you have no limit, then arrays are the way to go. Otherwise if you say had a limit of 20 numbers, zednas may be the way to go. What the example does there is you have 20 parameters. The macro @NumParams returns how many parameters were in the function call. Edited December 20, 2008 by BrettF Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 20, 2008 Moderators Share Posted December 20, 2008 AutoIt doesn't provide an ellipsis type parameter, so you'll either have to make a ton of conditional parameters as Zedna suggested, or pass them as an array as Brett suggested. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Ultima2 Posted December 20, 2008 Author Share Posted December 20, 2008 (edited) Oh ok thank you, I guess I will have to write a lot of $x = 0 in my parameter then D: Edited December 20, 2008 by Ultima2 Link to comment Share on other sites More sharing options...
ProgAndy Posted December 20, 2008 Share Posted December 20, 2008 To create it easily: $Num = Int(InputBox("Number of params","How many Params to you want to have?")) $code = "Func _ManyParamFunc( _ " & @CRLF & @TAB For $i = 1 To $Num $code &= " $Pm" & $i & "=0," Next $code = StringTrimRight($code,1) & " _ " & @CRLF & @TAB & ")" & @CRLF $code &= " Local $ParamArray" & @CRLF $code &= " If @NumParams Then "& @CRLF $code &= " Local $ParamArray["&$Num&"]=[" For $i = 1 To $Num $code &= "$Pm" & $i & "," Next $code = StringTrimRight($code,1) & "]"& @CRLF $code &= " ReDim $ParamArray[@NumParams]" & @CRLF $code &= " EndIf" &@CRLF $code &= " " &@CRLF $code &= "EndFunc" &@CRLF ConsoleWrite($code) If MsgBox(36,"many params","Put to clip?")=7 Then ClipPut($code) *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
BrettF Posted December 20, 2008 Share Posted December 20, 2008 Different Method.... Global $param5 $param0 = 1 $param1 = 2 $param2 = 3 $param3 = 4 $param4 = 5 $output = _Add ("param", 4, 0) MsgBox (0, "", $output) Func _Add ($name, $params, $start = 0) Local $ret For $i = $start to $params $ret += Eval ($name & $i) Next Return $ret EndFunc Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now