What's passed by Value/Reference by default?

Monkey Forums/Monkey Programming/What's passed by Value/Reference by default?

Nobuyuki(Posted 2012) [#1]
Simple topic, when doing a function or method call, what types are passed by value and what types are passed by reference as default in monkey? Does it vary based on target platform?

My working assumption has been that Objects are passed byRef, and everything else is passed ByVal. I've been afraid to test this assumption on non-Object primitives, however, because I don't want to sneak nasty bugs in my code on the off chance everything's passed in by reference as default. Of particular curiosity to me is whether passing in a String (which is immutable) is by reference, and if "changing" the string on the method level changes the reference outside that scope. I've tested it only with Objects, and know that operating directly on variables inside the method scope effect references from the caller.

A cursory ctrl+f of the monkey language reference didn't reveal anything specific.


ziggy(Posted 2012) [#2]
nothing is passed by reference. Monkey does not support it that's why we have boxing classes. not a big issue tho.


Beaker(Posted 2012) [#3]
Ziggy is correct. But, if I understand you correctly, then yes only Objects (including Arrays) contents are changeable inside functions. Strings, Ints, Floats, Bools are not.

Hope that helps.


ziggy(Posted 2012) [#4]
Ah yes, you can modify any class instance content but not the instance being referenced, I hope this makes it clear:
[monkeycode]Function Main()

Local test:= New TestClass
test.value = "default value"

Print "Before function:" + test.value 'Outputs "default value"
TestFunction(test)

Print "After function:" + test.value 'outputs "modified value"

End


Function TestFunction(parameter:TestClass)

Print "Begining Function: " + parameter.value 'outputs "default value"

parameter.value = "modified value"
Print "Middle function: " + parameter.value 'outputs "modified value"


parameter = New TestClass 'We modify the instance being pointed here

parameter.value = "Another thing"
Print "End function: " + parameter.value 'outputs "Another thing"


End

Class TestClass
Field value:String
End[/monkeycode]

This sample outputs:
Before function:default value
Begining Function: default value
Middle function: modified value
End function: Another thing
After function:modified value



Nobuyuki(Posted 2012) [#5]
Yes, thank you everyone, that answers my question. Beaker introduced another question to me -- do you have to make a copy of an array to directly interact with its contents without passing the changes back to the caller? (ie: does it behave like Objects and generic containers?) Or, again, does it depend on the array type? Specifically, I'm concerned about arrays of type primitives such as Float.


Beaker(Posted 2012) [#6]
You can permanently change the contents of a Float array inside a function no problem, without having to copy it or pass it back. Easy to test.


Gerry Quinn(Posted 2012) [#7]
What I like to do is name parameters that can be changed with an underline in front. So the method:

DoSomething( arr:int[] )

..promises not to change arr. If it will change the passed array, its signature will be:

DoSomething( _arr:int[] )

Of course the compiler does not enforce this like const is enforced in C++, but it is fine so long as you obey your own rules.