Jump to content

Optional and infiniti amount of parameters


James
 Share

Recommended Posts

Hey all,

How can I a make a function in a UDF accept any amount of parameters?

Look, I have the following:

Func Avg($avg_1, $avg_2, $avg_3, $avg_4, $amount)
       $add = $avg_1 + $avg_2 + $avg_3 + $avg_4
       $final = $add / $amount
       Return $final
EndFunc

I need the $avg_# to add as many as possible, and the actual function to calculate it all.

-James

Link to comment
Share on other sites

Hey all,

How can I a make a function in a UDF accept any amount of parameters?

Look, I have the following:

Func Avg($avg_1, $avg_2, $avg_3, $avg_4, $amount)
       $add = $avg_1 + $avg_2 + $avg_3 + $avg_4
       $final = $add / $amount
       Return $final
EndFunc

I need the $avg_# to add as many as possible, and the actual function to calculate it all.

-James

You wouldn't.

I would put all my values in to an array and pass that to a function to work out the averages

Local $myArray[11]

$myArray[1] = 22
$myArray[2] = 100
$myArray[3] = 9
$myArray[4] = 4
$myArray[5] = 33
$myArray[6] = 12
$myArray[7] = 33
$myArray[8] = 2
$myArray[9] = 200
$myArray[10] = 12


$averages = AVG($myArray)

Msgbox(0,"Average",$Averages)



Func Avg($aArrayOfValues)
    Local $vTotal, $final,$amount
    
    $amount = Ubound($aArrayOfValues) -1; total number of entries
       For $i = 1 to $amount
           $vTotal += $aArrayOfValues[$i]
       Next
       
       $final = $vTotal / $amount
       Return $final
EndFunc
Link to comment
Share on other sites

  • Moderators

You wouldn't.

I would put all my values in to an array and pass that to a function to work out the averages

Local $myArray[11]

$myArray[1] = 22
$myArray[2] = 100
$myArray[3] = 9
$myArray[4] = 4
$myArray[5] = 33
$myArray[6] = 12
$myArray[7] = 33
$myArray[8] = 2
$myArray[9] = 200
$myArray[10] = 12
$averages = AVG($myArray)

Msgbox(0,"Average",$Averages)
Func Avg($aArrayOfValues)
    Local $vTotal, $final,$amount
    
    $amount = Ubound($aArrayOfValues) -1; total number of entries
       For $i = 1 to $amount
           $vTotal += $aArrayOfValues[$i]
       Next
       
       $final = $vTotal / $amount
       Return $final
EndFunc
Or you could create the array from a string:
Global $myVargs = _MyFunc("abc|def|1|2|##", "|")
Func _MyFunc($sArgs, $vDelim = ",")
    Local $avArray[2]
    If StringInStr($sArgs, $vDelim) Then
        $avArray = StringSplit($sArgs, $vDelim)
    Else
        $avArray[0] = 1
        $avArray[1] = $sArgs
    EndIf
    #cs
    ;Rest of whatever here: ie...
    For $iCC = 1 TO $avArray[0]
        ;do whatever
    NEXT
    #ce
EndFunc

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

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...