LoginSignup
100
78

"call by reference"ではない動作を「参照渡し」と言っている記事まとめ

Last updated at Posted at 2019-04-20

C++、C#、PHP等には"call by reference"という機能があります。ですが、この"call by reference"ではない動作を「参照渡し」と言っている記事をまとめました。対象には表記揺れにすぎない「参照呼び」や「参照呼び出し」も含めています。

他にもある、とか、実は否定しているとかあればコメントや修正依頼をください。ただし、追記や脚注など目立たない形で「実はそうは言わない」などと補足があったり、コメント等でそのような指摘があっても、全ての読者がそこまで細かく見るとは限らないため、除外しません。つまり、厳密には違うとか、機能ではなく動作のことを言っているとか色々言い訳を付けていても、表面だけ読んでいると「『参照渡し』と言っても良い」と読み手が感じられそうであれば、対象としています。

"call by reference"な動作とは?

定義や詳しい動作の解説はここではしませんが、"call by reference"になっている場合、仮引数そのものへの代入には下記のような副作用が伴います。このような動作になるかどうかで"call by reference"であるかどうかを判断しています。

C++
#include <iostream>
void f(int& x)
{
    x = 1;
}
int main()
{
    int a = 0;
    f(a);
    std::cout << a << std::endl; // 1
    return 0;
}

記事一覧

以下の記事は"call by reference"ではない動作、ほとんどの場合は"call by value"の一種である"call by address"または"call by sharing"と言われている動作を「参照渡し」と言っています。なお、本来、このような評価戦略の話は関数へ実引数へ渡す時の動作のことを言います。しかし、関数へ実引数を渡すことは、言い換えると、仮引数に実引数を代入(assignment)する事でもあります。よって、通常の代入に対する解説でも、関数への仮引数への代入時と同等の動作の解説として考え、どの評価戦略になるのかを判断しています。

検索の関係上、記事は言語別で、C、JavaScript、Java、Python、Rubyに絞りました。これらの言語には"call by reference"の機能は存在しません。また、スタック・オーバーフローやteratail等のQAサイトは除外しています。

なお、ようなとか、正確ではないとか、言いながらも、結局頑なに「参照渡し」という言葉を使い続ける記事も含まれます。

C

JavaScript

TypeScript等のAltJSを含みます。

Java

8.4.1. Formal Parameters - Chapter 8. Classes - The Java® Language Specification Java SE 12 Edition より

When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor.

Python

4.6. Defining Functions - 4. More Control Flow Tools — Python 3.7.3 documentation より

The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object).

Ruby

Stack Overflowの回答によると、O'Reilly Media, Inc. The Ruby Programming Language (David Flanagan, Yukihiro Matsumoto著) 3.8.1: Object References より

When you pass an object to a method in Ruby, it is an object reference that is passed to the method. It is not the object itself, and it is not a reference to the reference to the object. Another way to say this is that method arguments are passed by value rather than by reference, but that the values passed are object references.

Crystal

  1. C++の"call by reference"は「参照渡し」では無いという主張をしている。

  2. 原文でも"objects and arrays in JavaScript are passed by reference"と書かれているため、誤訳ではありません。

  3. 修正版では「参照渡し」の表現はありません。

  4. 原文は確認できませんでしたが、"Peter J. Jones, author of Effective Ruby: 48 Specific Ways to Write Better Ruby, examines all sides of the great 'call-by-value or call-by-reference' argument-passing debate."とあるため、原文でも"call by reference"と言っている可能性が高いです。

100
78
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
100
78