Jump to content

Need help with User Defined Functions


Ultima2
 Share

Recommended Posts

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 by Ultima2
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by BrettF
Link to comment
Share on other sites

  • Moderators

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

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

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
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...