passing an array to a function

BlitzMax Forums/BlitzMax Beginners Area/passing an array to a function

JBR(Posted 2009) [#1]
Is this the best way to do it?

Global A_Array1:Float[ 100, 100 ]
Global A_Array2:Float[ 100, 100 ]

process_array( A_Array1 )

Print A_Array1[0,0]
Print A_Array2[0,0]

process_array( A_Array2)

Print A_Array1[0,0]
Print A_Array2[0,0]



End


Function process_array:Int( array:Float[,] )

	array[ 0, 0 ] = 123.456


End Function



Htbaa(Posted 2009) [#2]
Haven't tested this but it looks to me like this doesn't do anything?

Try passing the array as a reference, something like

Function process_array:Int( array:Float[,] Var)
array[ 0, 0 ] = 123.456
End Function



JBR(Posted 2009) [#3]
Hi, works the same with/without the Var.

Don't know why but maybe to save copying the array?

Jim


Htbaa(Posted 2009) [#4]
Just tested it and it seems like BlitzMax handles passed arrays by reference, just like it does with objects.

Regular types like integers, floats, doubles and strings are only passed by reference if you add Var in the function declaration.


JBR(Posted 2009) [#5]
Thanks


Otus(Posted 2009) [#6]
There's a difference between using Var and not, but it isn't the one Htbaa said at first. If you use Var, you can also replace the array with another one. The following resizes the array, but wouldn't work without Var:

Function Resize(arr:Float[] Var)
  arr = arr[..arr.length*2]
End Function



Htbaa(Posted 2009) [#7]
Thanks. I didn't know about that one, as I can't find it anywhere in the BlitzMax documentation...