Jump to content



Photo

Pass an array to a function?


  • Please log in to reply
3 replies to this topic

#1 Shastaman

Shastaman

    Seeker

  • Active Members
  • 18 posts

Posted 18 March 2008 - 08:37 PM

Trying to pass an array to a function, but it doesn't like function declaration. Realize the example declares it as global but I want to pass in different arrays so I don't want to specify specific array name in function.

Dim $bob[2]

$bob[0] = "Hey 1"
$bob[1] = "Hey 2"

SendArray($bob)


Func SendArray($funcArray[])

MSgbox(0, "funcArray 0 in function", $funcArray[0])
MSgbox(0, "funcArray 1 in function", $funcArray[1])

EndFunc







#2 Nahuel

Nahuel

    To Err is human, to Arr is pirate.

  • Active Members
  • PipPipPipPipPipPip
  • 1,841 posts

Posted 18 March 2008 - 08:55 PM

Dim $bob[2] $bob[0] = "Hey 1" $bob[1] = "Hey 2" SendArray($bob) Func SendArray($funcArray) MSgbox(0, "funcArray 0 in function", $funcArray[0]) MSgbox(0, "funcArray 1 in function", $funcArray[1]) EndFunc


It just has to work. Remember that arrays are variables too. So no need for the "[]" when passing the whole array.

#3 Zedna

Zedna

    AutoIt rulez!

  • MVPs
  • 8,409 posts

Posted 18 March 2008 - 09:07 PM

One note:
Consider ByRef for speed optimization reason.

Func SendArray($funcArray)

Func SendArray(ByRef $funcArray)

If you don't use ByRef then input array is COPIED to local variable (param)
If you use ByRef then no COPY is performed.

#4 Shastaman

Shastaman

    Seeker

  • Active Members
  • 18 posts

Posted 18 March 2008 - 09:41 PM

Thanks.

I thought I would have to specify I was passing an array but, likely due to the variant type, it doesn't need this specificity.

Thanks all




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users