Pass parameter by reference (var)?

Monkey Forums/Monkey Programming/Pass parameter by reference (var)?

Grey Alien(Posted 2013) [#1]
Hi all, so in BlitzMax you could pass a parameter into a function/method and put var after it so that you could alter it and the calling function could use the result. Very useful.

I can't find anything like this in monkey, is the accepted method to pass in an object now and alter that even for plain ints/strings etc?


Goodlookinguy(Posted 2013) [#2]
Yes to your bottom question. Use BoxInt, BoxBool, BoxString, and BoxFloat to put the primary datatypes into an object. Then use UnboxInt, UnboxBool, UnboxString, and UnboxFloat, respectively, to change them back to the primary datatypes. There's also ArrayBoxer<DataType>.Box and ArrayBoxer<DataType>.Unbox for passing arrays.


Samah(Posted 2013) [#3]
I normally just cheat and use an array. I would assume it's a little more lightweight (depending on target).
[monkeycode]Function SomeFunction(arg:Float[])
arg[0] = 10
End[/monkeycode][monkeycode]Local arr:Float[] = New Float[1]
SomeFunction(arr)
Print arr[0][/monkeycode]
You could make the array global to avoid instantiating it on every call, but then you need to be careful with recursive calls.


Grey Alien(Posted 2013) [#4]
Both great suggestions thanks! The Array one is very sneaky :-)