Hacker News new | ask | show | jobs
by Kranar 1295 days ago
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.

1 comments

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.

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
  {}
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.
I think you are deeply confused about this topic and should not be making judgements about other people. Instead of making an honest attempt to understand the difference between pass by value and pass by reference, you are trying to mince words to save face and even your attempt to mince words is incorrect.

While I likely can not convince you further that you are in error, especially since you're now trying to double down on this, for others reading this who have a genuine desire to understand this topic, please understand that Java, JavaScript and Python do not pass by reference, but instead pass by value and refrain from attempting to redefine technical terminology.

The references I cite are quite authoritative and unambiguous on this topic.

I already agreed you were technically correct!