Jump to content

Variable number of params in Function


Recommended Posts

I looked in the FAQ and Other forums. Found some old (2004) questions.

Help and documentation is not clear to me

But ,

Is it possible to pass a variable numbers of params to a function.

Not sure what you mean.. Possibly something like this:

Func Somefunc($Somevariable)
MsgBox( 0, "", $Somevariable )
EndFunc
Somefunc(5)
Edited by Qousio
Link to comment
Share on other sites

Not sure what you mean.. Possibly something like this:

Func Somefunc($Somevariable)
MsgBox( 0, "", $Somevariable )
EndFunc
Somefunc(5)

Nope I want to code this

$x= myfunc($a,$b )

or

$x= myfunc($a,$b,$c)

functiom myfunc( ???)

some coding

return($result)

endfunc

Edited by wimhek
Link to comment
Share on other sites

Nope I want to code this

$x= myfunc($a,$b )

or

$x= myfunc($a,$b,$c)

functiom myfunc( ???)

some coding

return($result)

endfunc

Func functioname ( [Const] [byRef] $param1, ..., [Const] [byRef] $paramN, $optionalpar1 = value, ...)

...

[Return [value]]

EndFunc

Link to comment
Share on other sites

Func functioname ( [Const] [byRef] $param1, ..., [Const] [byRef] $paramN, $optionalpar1 = value, ...)

...

[Return [value]]

EndFunc

OK this is copied from the help. Problem is that I must know in advance how many params I am going to pass.

PS I dont want to use an array as param.

PS I dont want to us to pas one param with delimiterd e.g.

$a="one|two|three"

myfunc($a)

Any help much appreciated.

Link to comment
Share on other sites

OK this is copied from the help. Problem is that I must know in advance how many params I am going to pass.

PS I dont want to use an array as param.

PS I dont want to us to pas one param with delimiterd e.g.

$a="one|two|three"

myfunc($a)

Any help much appreciated.

I'm not sure if you quite understand the difference between passing and returning parameters...

Heres and example:

Func Func1($ B) 
 $a = 5
 $o = $B + $a
 Return $a + $o
EndFunc   ;==>Func1

Func Func2($c, $d)
 $g = $c + $d
 MsgBox(0, "", $g)
EndFunc   ;==>Func2

While 1
 Func2(3, Func1(3))
WEnd

Passing a parameter means we transfer the value of a local paramter to a different function... While returning means returning some value from a function, for example $a+$b+$c etc.

Although you can pass by returning aswell....

You can just use global parameters by the way. Just declare them outside of a function, or as Global $parameter.

Edited by Qousio
Link to comment
Share on other sites

... anyway

; Case 1
$X = _MyFunc()

ConsoleWrite($X & @CRLF)


; Case 2
$X = _MyFunc(7, 12)

ConsoleWrite($X & @CRLF)


; Case 3
$X = _MyFunc(1, 2, 3)

ConsoleWrite($X & @CRLF)


; Case 4
$X = _MyFunc(1, 2, 3, 965)

ConsoleWrite($X & @CRLF)


; Case 5
$X = _MyFunc(112, 2, 3, 355, -472)

ConsoleWrite($X & @CRLF)






Func _MyFunc($A = 0, $B = 0, $C = 0, $D = 0, $E = 0)

    Local $iResult = $A + $B + $C + $D + $E

    Return $iResult

EndFunc   ;==>_MyFunc
Link to comment
Share on other sites

I'm not sure if you quite understand the difference between passing and returning parameters...

Heres and example:

Func Func1($ B) 
$a = 5
$o = $b+$a
Return $a+$o
EndFunc

Func Func2($c, $d)
$g = $c+$d
MsgBox(0, "", $d )
EndFunc

While 1
Func2(3, Func1(3))
Wend

Passing a parameter means we transfer the value of a local paramter to a different function... While returning means returning some value from a function, for example $a+$b+$c etc.

Although you can pass by returning aswell....

You can just use global parameters by the way. Just declare them outside of a function, or as Global $parameter.

OK let me try to explain.

I want to create a function , and I want to pass a variable number of variables to that function.

e.g.

$rc=Myfunc($a)

...

$rc=Myfunc($a,$b,$c)

....

$rc=Myfunc($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p)

Link to comment
Share on other sites

OK let me try to explain.

I want to create a function , and I want to pass a variable number of variables to that function.

e.g.

$rc=Myfunc($a)

...

$rc=Myfunc($a,$b,$c)

....

$rc=Myfunc($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p)

I guess you don't quite understand what a variable is... You can't put 10 variables in one, unless you have declared a special type of variable. I am not sure if this is possible in Autoit.

You could do, $rc[0]=$a, $rc[1]=$b etc... But $rc=$a,$b,...$n does not make sense. How do you even imagine this? If $a,$b etc. is text and you want to combine it, you could juse use $a & $b & $c & $d etc. If its integers\reals then you would do $rc=$a+$b+$c etc., But $rc=$a,$b does not make sense... Perhaps if you explained what type $a, $b, $c etc. is ? Are they strings? What are you trying to achieve when outputting $rc ?

Edited by Qousio
Link to comment
Share on other sites

... anyway

; Case 1
$X = _MyFunc()

ConsoleWrite($X & @CRLF)


; Case 2
$X = _MyFunc(7, 12)

ConsoleWrite($X & @CRLF)


; Case 3
$X = _MyFunc(1, 2, 3)

ConsoleWrite($X & @CRLF)


; Case 4
$X = _MyFunc(1, 2, 3, 965)

ConsoleWrite($X & @CRLF)


; Case 5
$X = _MyFunc(112, 2, 3, 355, -472)

ConsoleWrite($X & @CRLF)






Func _MyFunc($A = 0, $B = 0, $C = 0, $D = 0, $E = 0)

    Local $iResult = $A + $B + $C + $D + $E

    Return $iResult

EndFunc   ;==>_MyFunc

Link to comment
Share on other sites

OK the test prototype so far : (thanks anyway for yout patience)

{{{

$a="ape"

$b="nut"

$X=tabelregel($a,$b,"another")

consolewrite ($X & @CRLF)

Func tabelregel($aa[1]="$$",$aa[2]="$$",$aa[3]="$$",$aa[4]="$$",$aa[5]="$$",$aa[6]="$$",$aa[7]="$$",$aa[8]="$$",$aa[9]="$$",$aa[10]="$$",$aa[11]="$$",$aa[12]="$$",$aa[13]="$$")

local $x = "<tr>"

local $y

for $y = 1 to @NumParams

$x = $x & "<td>" & $aa[$y] & "</td>"

Next

return $x & "</tr>"

EndFunc

}}}

Edited by wimhek
Link to comment
Share on other sites

Found it , eval did the trick

{{{

$a="ape"

$b="nut"

$X=tabelregel($a,$b,"another")

consolewrite ($X & @CRLF)

Func tabelregel($aa1="$$",$aa2="$$",$aa3="$$",$aa4="$$",$aa5="$$",$aa6="$$",$aa7="$$",$aa8="$$",$aa9="$$",$aa10="$$" )

local $x = "<tr>"

local $y

for $y = 1 to @NumParams

$x = $x & "<td>" & eval("aa" & $y) & "</td>"

Next

return $x & "</tr>"

EndFunc

}}}

Link to comment
Share on other sites

If understand correctly, you are looking for something like VB's ParamArray. or perl's list passing, where the user can specify a variable number of parameters in the call to a function and then have a mechanism in the definition of the function to read each of those parameters, probably an array. Unfortunately, we have not implemented that behaviour in AutoIt. The most straight-forward way to do something similar would be to pass an array.

Func MinVal(Const ByRef $vArray)
    Local $vMinVal, $iIndex

    If UBound($vArray, 0) = 0 Then
        ; Not an array, so just return the value.
        return $vArray
    ElseIf UBound($vArray, 0) > 1 Then
        ; This routine is set up for one-dimensional arrays only.
        SetError(1, UBound($vArray))
    EndIf
    $vMinVal = $vArray[0]
    For $iIndex = 1 To UBound($vArray)
        If $vMinVal > $vArray[$iIndex] Then $vMinVal = $vArray[$iIndex]
    Next
    Return $vMinVal
EndFunc

Global $k[14]

$k[0] = 8
$k[1] = 3
.
.
.
$k[13] = 6.279

MsgBox(0, "Testing: Smallest", MinVal($k) )

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

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