All those languages pass by value and it's kind of embarrassing that you mixed it up while criticizing someone else for not knowing it.
One necessary (but insufficient) test you can use to determine if your language has call by reference or call by value is whether you can implement a swap function. In the languages you list, a swap function is not possible to implement, whereas in C++ it is, which tells you that those languages do not implement pass by reference.
We are actually both wrong. If they passed objects by value you'd be modifying a copy. They technically "pass by object reference" as another poster mentioned.
No sorry, you are incorrect and you can verify the correct answer on Wikipedia or any technical book on this subject.
For example the Java Language Specification states in section 8.4.1 that Java is pass by value: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html... ECMAScript's Language Specification also states in section 10.6 that it uses pass by value semantics, although it's much more formal about the specific approach it uses: https://262.ecma-international.org/5.1/ I can't link to the specific section but you can review the semantics of MakeArgGetter and MakeArgSetter which are specified to produce arguments bound to the *value* associated with the name, as opposed to a reference. Python does not have a spec that I can reference, but given that its argument semantics follows those of Java, and once again the inability to write a swap function, it should not be too difficult to deduce that Python also passes by value.
I said they pass objects by reference, which is true. Technically, yes, Java is pass by value. However, objects are not passed by value. The value you are passing is a reference (pointer) to that object. If this were not the case, you would not be able to see modifications to method parameters reflected in the caller because you're modifying a copy. You are not modifying a copy.
This is indeed correct. Object references themselves in Java/Python are passed by value, which is why the following two code samples do not have the same effect:
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(d):
... d['foo'] 'bar'}
...
>>> a = {}
>>> f(a)
>>> a
{'foo': 'bar'}
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(d):
... d = {'foo': 'bar'}
...
>>> a = {}
>>> f(a)
>>> a
{}
One necessary (but insufficient) test you can use to determine if your language has call by reference or call by value is whether you can implement a swap function. In the languages you list, a swap function is not possible to implement, whereas in C++ it is, which tells you that those languages do not implement pass by reference.
For example the Java Language Specification states in section 8.4.1 that Java is pass by value:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html...
ECMAScript's Language Specification also states in section 10.6 that it uses pass by value semantics, although it's much more formal about the specific approach it uses:
https://262.ecma-international.org/5.1/
I can't link to the specific section but you can review the semantics of MakeArgGetter and MakeArgSetter which are specified to produce arguments bound to the *value* associated with the name, as opposed to a reference.
Python does not have a spec that I can reference, but given that its argument semantics follows those of Java, and once again the inability to write a swap function, it should not be too difficult to deduce that Python also passes by value.
Continue this thread — 2 replies