Page 1 of 1

passing an array into a function?

Posted: 04 Apr 2020, 13:31
by awcrt9316
I need some way to pass an array to a function. Does anyone know how to do that?

Code: Select all

c := a7
array := {}
array["red"] := "a7"
array["orange"] := "a6"
array["blue"] := "a5"
green:= 1
purple:=0

movementdetector(green, purple, c)

movementdetector(a, b, c)
{
	if (a = 1)&&(b = 0) 
	{
	for index, element in array
	if (element = c)
	{
		MsgBox, % "Item " index " has a y value of " othervar
        }

	}


Re: passing an array into a function?

Posted: 04 Apr 2020, 14:04
by Chunjee
Not sure what the rest of the variables are doing, but your array seems to be called "array" so functionname(array) will suffice

Re: passing an array into a function?  Topic is solved

Posted: 04 Apr 2020, 14:12
by boiler
See below. Note that I changed the name of the array in the function thisarray to make it clear that it's not really using the same name as the array you passed to it (although you could name it the same). It is passing a reference to the same array object. If you want the function to have its own copy of the array so that you could change it without changing the original, you would have to Clone() it.

Note that a couple errors were fixed: Added quotes in c := "a7" and replaced othervar with c because othervar wasn't referenced anywhere else.

Code: Select all

c := "a7"
array := {}
array["red"] := "a7"
array["orange"] := "a6"
array["blue"] := "a5"
green:= 1
purple:=0

movementdetector(green, purple, c, array)

movementdetector(a, b, c, thisarray)
{
	if (a = 1)&&(b = 0) 
	{
	for index, element in thisarray
	if (element = c)
	{
		MsgBox, % "Item " index " has a y value of " c
        }

	}
}