两者有什么区别
引用传递的参数 参数通过value?
你能给我举几个例子吗?
两者有什么区别
引用传递的参数 参数通过value?
你能给我举几个例子吗?
当前回答
1. 按值传递/按值调用
void printvalue(int x)
{
x = x + 1 ;
cout << x ; // 6
}
int x = 5;
printvalue(x);
cout << x; // 5
在按值调用中,当你将一个值传递给printvalue(x)时,即参数5,它被复制为void printvalue(int x)。现在,我们有两个不同的值5和复制的值5,这两个值存储在不同的内存位置。因此,如果你在void printvalue(int x)内做了任何更改,它不会反射回实参。
2. 引用传递/引用调用
void printvalue(int &x)
{
x = x + 1 ;
cout << x ; // 6
}
int x = 5;
printvalue(x);
cout << x; // 6
在引用调用中,只有一个区别。我们使用&,即地址操作符。通过做 Void printvalue(int &x)我们引用的是x的地址,这告诉我们它指向相同的位置。因此,在函数内部所做的任何更改都会在函数外部反映出来。
既然你来了,你也应该知道……
3.按指针传递/按地址调用
void printvalue(int* x)
{
*x = *x + 1 ;
cout << *x ; // 6
}
int x = 5;
printvalue(&x);
cout << x; // 6
在传递地址中,指针int* x保存传递给它的地址printvalue(&x)。因此,在函数内部所做的任何更改都会在函数外部反映出来。
其他回答
A major difference between them is that value-type variables store values, so specifying a value-type variable in a method call passes a copy of that variable's value to the method. Reference-type variables store references to objects, so specifying a reference-type variable as an argument passes the method a copy of the actual reference that refers to the object. Even though the reference itself is passed by value, the method can still use the reference it receives to interact with—and possibly modify—the original object. Similarly, when returning information from a method via a return statement, the method returns a copy of the value stored in a value-type variable or a copy of the reference stored in a reference-type variable. When a reference is returned, the calling method can use that reference to interact with the referenced object. So, in effect, objects are always passed by reference.
In c#, to pass a variable by reference so the called method can modify the variable's, C# provides keywords ref and out. Applying the ref keyword to a parameter declaration allows you to pass a variable to a method by reference—the called method will be able to modify the original variable in the caller. The ref keyword is used for variables that already have been initialized in the calling method. Normally, when a method call contains an uninitialized variable as an argument, the compiler generates an error. Preceding a parameter with keyword out creates an output parameter. This indicates to the compiler that the argument will be passed into the called method by reference and that the called method will assign a value to the original variable in the caller. If the method does not assign a value to the output parameter in every possible path of execution, the compiler generates an error. This also prevents the compiler from generating an error message for an uninitialized variable that is passed as an argument to a method. A method can return only one value to its caller via a return statement, but can return many values by specifying multiple output (ref and/or out) parameters.
请参阅c#讨论和示例链接文本
例子:
class Dog
{
public:
barkAt( const std::string& pOtherDog ); // const reference
barkAt( std::string pOtherDog ); // value
};
Const &通常是最好的。你不会受到建造和破坏的惩罚。如果引用不是const,你的接口暗示它将改变传入的数据。
如果你不想在将原始变量传递给函数后改变它的值,那么函数应该构造一个“按值传递”参数。
然后函数将只有值,而没有传入变量的地址。如果没有变量的地址,函数内部的代码就不能改变从函数外部看到的变量值。
但是如果你想要赋予函数从外部看到的改变变量值的能力,你需要使用引用传递。因为值和地址(引用)都是传递进来的,并且在函数内部可用。
当通过引用传递时,基本上是传递一个指向变量的指针。通过值传递,即传递变量的副本。
在基本用法中,这通常意味着通过引用传递,对变量的更改将在调用方法中看到,而在通过值传递时则不会。
这里的许多答案(特别是被点赞最多的答案)实际上是不正确的,因为他们误解了“参考呼叫”的真正含义。我来解释一下。
博士TL;
简单来说:
按值调用意味着将值作为函数参数传递 引用调用意味着将变量作为函数参数传递
打个比方:
Call by value is where I write down something on a piece of paper and hand it to you. Maybe it's a URL, maybe it's a complete copy of War and Peace. No matter what it is, it's on a piece of paper which I've given to you, and so now it is effectively your piece of paper. You are now free to scribble on that piece of paper, or use that piece of paper to find something somewhere else and fiddle with it, whatever. Call by reference is when I give you my notebook which has something written down in it. You may scribble in my notebook (maybe I want you to, maybe I don't), and afterwards I keep my notebook, with whatever scribbles you've put there. Also, if what either you or I wrote there is information about how to find something somewhere else, either you or I can go there and fiddle with that information.
“按值调用”和“按引用调用”不是什么意思
请注意,这两个概念都完全独立于引用类型的概念(在Java中引用类型是Object的所有子类型,在c#中是所有类类型),或者像C中那样的指针类型的概念(它们在语义上等价于Java的“引用类型”,只是语法不同)。
引用类型的概念对应于URL:它本身是一段信息,也是指向其他信息的引用(如果您愿意,也可以称为指针)。你可以在不同的地方有多个URL副本,它们不会改变它们链接到的网站;如果网站更新了,那么每个URL副本仍然会导致更新的信息。相反,在任何一个地方更改URL都不会影响URL的任何其他书面副本。
注意,c++有一个“引用”的概念(例如int&),它不像Java和c#的“引用类型”,而像“引用调用”。Java和c#的“引用类型”,以及Python中的所有类型,都类似于C和c++所说的“指针类型”(例如int*)。
好的,这里有一个更长更正式的解释。
术语
首先,我想强调一些重要的术语,以帮助澄清我的答案,并确保我们在使用词语时都指的是相同的想法。(在实践中,我相信绝大多数关于这类话题的困惑都源于使用词语的方式没有完全表达出想要表达的意思。)
首先,这里有一个类似c语言的函数声明示例:
void foo(int param) { // line 1
param += 1;
}
这里有一个调用这个函数的例子:
void bar() {
int arg = 1; // line 2
foo(arg); // line 3
}
通过这个例子,我想定义一些重要的术语:
foo是在第一行声明的函数(Java坚持使所有函数都是方法,但概念是一样的,但不失通用性;C和c++在声明和定义之间做了区分,我不会在这里深入讨论) Param是foo的正式形参,也在第1行声明 Arg是一个变量,具体来说是函数栏的一个局部变量,在第2行声明并初始化 Arg也是第3行特定foo调用的参数
这里有两组非常重要的概念需要区分。第一个是值对变量:
值是对语言中的表达式求值的结果。例如,在上面的bar函数中,在行int arg = 1;之后,表达式arg的值为1。 变量是值的容器。变量可以是可变的(这是大多数类C语言的默认值),只读的(例如使用Java的final或c#的readonly声明)或深度不可变的(例如使用c++的const)。
另一对需要区分的重要概念是参数和参数:
形参(也称为形式形参)是调用函数时必须由调用方提供的变量。 实参是由函数的调用者提供的值,以满足该函数的特定形式形参
按值调用
在按值调用中,函数的形式形参是为函数调用新创建的变量,并使用它们的实参值进行初始化。
这与任何其他类型的变量用值初始化的方式完全相同。例如:
int arg = 1;
int another_variable = arg;
这里arg和another_variable是完全独立的变量——它们的值可以彼此独立地变化。然而,在声明another_variable时,它被初始化为与arg持有相同的值——即1。
因为它们是自变量,所以对another_variable的修改不会影响arg:
int arg = 1;
int another_variable = arg;
another_variable = 2;
assert arg == 1; // true
assert another_variable == 2; // true
这与上面例子中的arg和param之间的关系完全相同,为了对称起见,我将在这里重复一遍:
void foo(int param) {
param += 1;
}
void bar() {
int arg = 1;
foo(arg);
}
就像我们这样写代码一样:
// entering function "bar" here
int arg = 1;
// entering function "foo" here
int param = arg;
param += 1;
// exiting function "foo" here
// exiting function "bar" here
也就是说,按值调用的定义特征是被调用方(在本例中为foo)接收值作为参数,但是从调用方(在本例中为bar)的变量中为这些值拥有自己的单独变量。
回到我上面的比喻,如果我是bar,你是foo,当我打电话给你时,我会递给你一张写着值的纸。你把这张纸叫做参数。这个值是我在我的笔记本上写的值(我的局部变量)的副本,在一个我称之为arg的变量中。
(顺便说一句:根据硬件和操作系统的不同,如何从一个函数调用另一个函数有不同的调用约定。调用约定就像我们决定是我把值写在一张纸上,然后交给你,还是你有一张纸,我把值写在上面,还是我写在我们面前的墙上。这也是一个有趣的话题,但远远超出了这个已经很长的答案的范围。)
引用调用
在引用调用中,函数的形参只是调用者作为参数提供的相同变量的新名称。
回到上面的例子,它相当于:
// entering function "bar" here
int arg = 1;
// entering function "foo" here
// aha! I note that "param" is just another name for "arg"
arg /* param */ += 1;
// exiting function "foo" here
// exiting function "bar" here
因为param只是arg的另一个名字——也就是说,它们是同一个变量,对param的更改会反映在arg中。这是按引用调用不同于按值调用的基本方式。
很少有语言支持引用调用,但c++可以这样做:
void foo(int& param) {
param += 1;
}
void bar() {
int arg = 1;
foo(arg);
}
在这种情况下,param不只是与arg有相同的值,它实际上是arg(只是名字不同),所以bar可以观察到arg被增加了。
请注意,这不是Java、JavaScript、C、Objective-C、Python或几乎任何其他流行语言的工作方式。这意味着这些语言不是通过引用调用的,而是通过值调用的。
附录:通过对象共享调用
如果你拥有的是按值调用,但实际值是引用类型或指针类型,那么“值”本身就不是很有趣(例如,在C中它只是一个特定于平台大小的整数)——有趣的是该值指向什么。
如果该引用类型(即指针)所指向的对象是可变的,那么可能会出现一个有趣的效果:您可以修改指向值,而调用方可以观察到指向值的变化,即使调用方无法观察到指针本身的变化。
再次借用URL的类比,如果我们都关心的是网站而不是URL,那么我给你一个网站URL的副本就不是特别有趣了。事实上,你在你的URL副本上涂鸦并不会影响我的URL副本,这不是我们关心的事情(事实上,在Java和Python等语言中,“URL”或引用类型值根本不能修改,只有它指向的东西可以修改)。
Barbara Liskov在发明CLU编程语言(具有这些语义)时,意识到现有的术语“按值调用”和“按引用调用”对于描述这种新语言的语义并不是特别有用。所以她发明了一个新术语:对象共享调用。
当讨论技术上按值调用的语言,但其中常用的类型是引用或指针类型(即:几乎所有现代命令式、面向对象或多范式编程语言)时,我发现简单地避免谈论按值调用或按引用调用会少得多混乱。坚持按对象共享调用(或简单地按对象调用),没有人会混淆。: -)