passing an array to a function
BlitzMax Forums/BlitzMax Beginners Area/passing an array to a function
| ||
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 |
| ||
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 |
| ||
Hi, works the same with/without the Var. Don't know why but maybe to save copying the array? Jim |
| ||
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. |
| ||
Thanks |
| ||
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 |
| ||
Thanks. I didn't know about that one, as I can't find it anywhere in the BlitzMax documentation... |