Why shouldn't we use the word "pass by reference" in Java, JavaScript, Python, Ruby, etc.?

The only reason I think is "because it is confused with" passing by reference "in C ++, C #, PHP, etc."

Java, Python, Ruby, JavaScript (ECMAScript), etc. are mentioned, but in this article, Java is mainly taken up as a representative of them. However, the same can be said for other languages, so the word "pass by reference" should not be used either.

What is true pass by reference?

This is an example of passing by reference in C ++.

#include <iostream>
using namespace std;
void f(int x) {
  x = 1;
}
void g(int &x) {
  x = 2;
}
int main(void) {
  int n = 0;
  cout << n << endl; // => 0
  f(n);
  cout << n << endl; // => 0
  g(n);
  cout << n << endl; // => 2
}

f is passed by value and g is passed by reference. By value, assignment to a formal argument has no effect on the actual argument, but by reference, assignment to a formal argument means that it is assigned to the actual argument itself. By reference, formal arguments can also be considered as aliases for actual arguments.

What should I do if I get confused?

This is because the person who was told that Java reference type passing is "passing by reference" cannot understand the above code when looking at passing by reference in C ++. In Java, even if the formal argument is a reference type, the actual argument cannot be changed by assigning the formal argument itself. In other words, it is a completely different behavior from what is called passing by reference in C ++. If you call it with the same name, you will not be able to understand which is the correct behavior.

Why are there so many people calling it "pass by reference" in Java?

In C ++, it is also expressed as a reference type. However, the meaning of "reference" is different from the Java reference type. This is because when you say "reference" in C ++, it means "reference to variable [^ 1]", but when you say "reference" in Java, it means "reference to (entity) of object" [^ 2]. It is probable that he reinvented the word "pass by reference" as a result of walking alone without understanding this difference.

Isn't it okay for the meaning to differ depending on the language?

Certainly, there are terms that have different meanings depending on the language and the community of the language, such as the "reference type" above. But in most cases, to avoid misunderstandings, the underlying meanings are common to each language and only depend on the language-specific circumstances. Unless it is strictly defined by specifications, it should be used in a language-independent sense as much as possible. In the first place, Java reference type passing is a completely different behavior from C ++ reference passing, and it is hard to say that it is fundamentally common.

Still, some may wonder if both are "passed by reference" because the languages are different. In that case, you should think about C #. In C #, the reference type is passed in the same way as Java, but it is also possible to pass by reference in C ++. If both Java and C ++ use the term "pass by reference", then in C # both different ways of passing can be referred to as "pass by reference". On the other hand, if one is called differently, the same behavior will have to be renamed depending on the language, which will cause further confusion and make it even more difficult to understand the behavior.

I'm not confused, so you can use it.

Even if you are good, the person who reads the article you wrote may be confused. People who don't know anything will be confused, at least if there are no notes. If you write such a confusing article, you have to say that you don't have the ability to write sentences that explain to others.

Still, I want to use the word "pass by reference"!

Please swear that you will not use languages such as C ++, C # and PHP for the rest of your life. Add the sentence "Only read to people who will never use another language with" passing by reference "in another sense, such as C ++, C #, PHP, etc." at the beginning of the article you write. It is recommended. That way, neither you nor the person who called your article will be confused forever.

Java is right and C ++ is wrong. What should be corrected is C ++.

The concept of passing by reference in C ++ is old and goes back at least to FORTRAN II. However, it is unknown whether it was called "pass by reference" from that time, but at least when the first C ++ was made, it seems that the word "pass by reference" was used to distinguish it from "pass by value". .. Java was created more than 10 years later. Words are the winner of the first person to call. It's difficult to change the meaning of a word that has existed before, and it will be difficult to change it to another name.

However, words change rapidly, so if you do your best to enlighten them, their meanings may change in about 10 years. I don't support or agree with you, so please do your best.

So what word should I use?

I mainly call it "pass by reference" or "pass by share", but I'm not sure if this is really all right. I asked a question on teratail, but I didn't get many answers. If you have an opinion that you should call it this way, please answer the following questions. Of course, you can still say that you should call it "pass by reference".

Name of evaluation strategy to pass (call) objects in a shareable manner

Reference: Don't call it by reference anymore

[^ 1]: To be exact, it is a reference to the lvalue. Also, since C ++ 11, there is another "reference" called an rvalue reference.

[^ 2]: Java reference types are close to C ++ pointer types. Since it is not a "reference" but a "reference value" that goes into a variable, there are things that are close to the actual value pointer. In contrast, a C ++ "reference" does not mean that a variable contains a pointer-like value.

Recommended Posts

Why shouldn't we use the word "pass by reference" in Java, JavaScript, Python, Ruby, etc.?
Why can I use the module by importing with python?
Express Python yield in JavaScript or Java
Read the file line by line in Python
Read the file line by line in Python
Divides the character string by the specified number of characters. In Ruby and Python.
How to use the C library in Python
Use the LibreOffice app in Python (3) Add library
Split camel case string word by word in Python