# Optional and infiniti amount of parameters

## 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
Return \$final
EndFunc```

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

-James

##### 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
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```

##### Share on other sites

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.

##### Share on other sites

Thanks Smoke. Thats just right. And pretty neat and tidy for me too. Thanks

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...