两者有什么区别
引用传递的参数 参数通过value?
你能给我举几个例子吗?
两者有什么区别
引用传递的参数 参数通过value?
你能给我举几个例子吗?
当前回答
It's a way how to pass arguments to functions. Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument. The value will be the same, but the identity - the variable - is different. Thus changes to a parameter done by the called function in one case changes the argument passed and in the other case just changes the value of the parameter in the called function (which is only a copy). In a quick hurry:
Java only supports pass by value. Always copies arguments, even though when copying a reference to an object, the parameter in the called function will point to the same object and changes to that object will be see in the caller. Since this can be confusing, here is what Jon Skeet has to say about this. C# supports pass by value and pass by reference (keyword ref used at caller and called function). Jon Skeet also has a nice explanation of this here. C++ supports pass by value and pass by reference (reference parameter type used at called function). You will find an explanation of this below.
代码
因为我的语言是c++,我将在这里使用它
// passes a pointer (called reference in java) to an integer
void call_by_value(int *p) { // :1
p = NULL;
}
// passes an integer
void call_by_value(int p) { // :2
p = 42;
}
// passes an integer by reference
void call_by_reference(int & p) { // :3
p = 42;
}
// this is the java style of passing references. NULL is called "null" there.
void call_by_value_special(int *p) { // :4
*p = 10; // changes what p points to ("what p references" in java)
// only changes the value of the parameter, but *not* of
// the argument passed by the caller. thus it's pass-by-value:
p = NULL;
}
int main() {
int value = 10;
int * pointer = &value;
call_by_value(pointer); // :1
assert(pointer == &value); // pointer was copied
call_by_value(value); // :2
assert(value == 10); // value was copied
call_by_reference(value); // :3
assert(value == 42); // value was passed by reference
call_by_value_special(pointer); // :4
// pointer was copied but what pointer references was changed.
assert(value == 10 && pointer == &value);
}
Java中的一个例子不会有什么坏处:
class Example {
int value = 0;
// similar to :4 case in the c++ example
static void accept_reference(Example e) { // :1
e.value++; // will change the referenced object
e = null; // will only change the parameter
}
// similar to the :2 case in the c++ example
static void accept_primitive(int v) { // :2
v++; // will only change the parameter
}
public static void main(String... args) {
int value = 0;
Example ref = new Example(); // reference
// note what we pass is the reference, not the object. we can't
// pass objects. The reference is copied (pass-by-value).
accept_reference(ref); // :1
assert ref != null && ref.value == 1;
// the primitive int variable is copied
accept_primitive(value); // :2
assert value == 0;
}
}
维基百科
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference
这家伙说得很准:
http://javadude.com/articles/passbyvalue.htm
其他回答
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)。因此,在函数内部所做的任何更改都会在函数外部反映出来。
下面是一个例子,演示了通过值-指针值-引用传递之间的区别:
void swap_by_value(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
void swap_by_pointer(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void swap_by_reference(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
int main(void){
int arg1 = 1, arg2 = 2;
swap_by_value(arg1, arg2);
cout << arg1 << " " << arg2 << endl; //prints 1 2
swap_by_pointer(&arg1, &arg2);
cout << arg1 << " " << arg2 << endl; //prints 2 1
arg1 = 1; //reset values
arg2 = 2;
swap_by_reference(arg1, arg2);
cout << arg1 << " " << arg2 << endl; //prints 2 1
}
“参照传递”方法有一个重要的局限性。如果参数声明为引用传递(因此前面有&号),其对应的实际参数必须是变量。
引用“按值传递”形式参数的实际参数一般可以是表达式,因此它不仅可以使用变量,还可以使用文字甚至函数调用的结果。
该函数不能将值放在变量以外的东西中。它不能将新值赋给文字或强制表达式更改其结果。
PS:你也可以在当前的线程中检查Dylan Beattie的答案,它用简单的语言解释了它。
首先,CS理论中定义的“通过值传递与通过引用传递”的区别现在已经过时了,因为最初定义为“通过引用传递”的技术已经不再受欢迎,现在很少使用
较新的语言2倾向于使用不同(但相似)的技术来实现相同的效果(见下文),这是混淆的主要来源。
第二个混淆的来源是,在“通过引用传递”中,“引用”的含义比一般术语“引用”更窄(因为这个短语比它更早)。
真实的定义是:
通过引用传递参数时,调用方和被调用方对参数使用相同的变量。如果被调用方修改了参数变量,则对调用方的变量可见。 当参数按值传递时,调用方和被调用方有两个具有相同值的自变量。如果被调用方修改了参数变量,则对调用方不可见。
在这个定义中需要注意的是:
"Variable" here means the caller's (local or global) variable itself -- i.e. if I pass a local variable by reference and assign to it, I'll change the caller's variable itself, not e.g. whatever it is pointing to if it's a pointer. This is now considered bad practice (as an implicit dependency). As such, virtually all newer languages are exclusively, or almost exclusively pass-by-value. Pass-by-reference is now chiefly used in the form of "output/inout arguments" in languages where a function cannot return more than one value. The meaning of "reference" in "pass by reference". The difference with the general "reference" term is that this "reference" is temporary and implicit. What the callee basically gets is a "variable" that is somehow "the same" as the original one. How specifically this effect is achieved is irrelevant (e.g. the language may also expose some implementation details -- addresses, pointers, dereferencing -- this is all irrelevant; if the net effect is this, it's pass-by-reference).
现在,在现代语言中,变量倾向于“引用类型”(另一个比“按引用传递”更晚发明的概念,并受到它的启发),即实际的对象数据被单独存储在某个地方(通常是在堆上),只有对它的“引用”被保存在变量中并作为参数传递
传递这样的引用属于值传递,因为从技术上讲,变量的值是引用本身,而不是被引用的对象。然而,对程序的最终影响可以是值传递或引用传递:
If a reference is just taken from a caller's variable and passed as an argument, this has the same effect as pass-by-reference: if the referred object is mutated in the callee, the caller will see the change. However, if a variable holding this reference is reassigned, it will stop pointing to that object, so any further operations on this variable will instead affect whatever it is pointing to now. To have the same effect as pass-by-value, a copy of the object is made at some point. Options include: The caller can just make a private copy before the call and give the callee a reference to that instead. In some languages, some object types are "immutable": any operation on them that seems to alter the value actually creates a completely new object without affecting the original one. So, passing an object of such a type as an argument always has the effect of pass-by-value: a copy for the callee will be made automatically if and when it needs a change, and the caller's object will never be affected. In functional languages, all objects are immutable.
正如你可能看到的,这对技术几乎与定义中的技术相同,只是在某种程度上间接地:只需将“变量”替换为“引用对象”。
它们没有统一的名称,这导致了一些扭曲的解释,比如“按值调用,其中值是引用”。1975年,Barbara Liskov提出了“按对象调用共享”(有时简称为“按对象调用共享”)这个术语,尽管它从未流行起来。此外,这两个短语都不能与原来的短语相提并论。难怪在没有更好的东西的情况下,旧的术语最终被重新使用,导致混乱
(对于新技术,我会使用术语“新的”或“间接的”值传递/引用传递。)
注:很长一段时间以来,这个答案都是这样说的:
Say I want to share a web page with you. If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself. If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.
这在很大程度上是正确的,除了狭义意义上的“引用”——它既是临时的又是隐式的(它不一定必须是临时的,但显式和/或持久性是额外的特性,不是引用传递语义的一部分,如上所述)。一个更接近的类比是给你一份文件的副本,而不是邀请你处理原件。
除非你用Fortran或Visual Basic编程,否则它不是默认行为,在现代使用的大多数语言中,真正的引用调用甚至是不可能的。
相当多的老年人也支持这一点
在一些现代语言中,所有类型都是引用类型。这种方法是由CLU语言在1975年首创的,后来被许多其他语言采用,包括Python和Ruby。还有更多的语言使用混合方法,其中一些类型是“值类型”,另一些是“引用类型”——其中包括c#、Java和JavaScript。
重复使用一个合适的旧术语本身并没有什么不好,但人们必须弄清楚每次使用的是什么意思。不这么做正是造成混乱的原因。
It's a way how to pass arguments to functions. Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument. The value will be the same, but the identity - the variable - is different. Thus changes to a parameter done by the called function in one case changes the argument passed and in the other case just changes the value of the parameter in the called function (which is only a copy). In a quick hurry:
Java only supports pass by value. Always copies arguments, even though when copying a reference to an object, the parameter in the called function will point to the same object and changes to that object will be see in the caller. Since this can be confusing, here is what Jon Skeet has to say about this. C# supports pass by value and pass by reference (keyword ref used at caller and called function). Jon Skeet also has a nice explanation of this here. C++ supports pass by value and pass by reference (reference parameter type used at called function). You will find an explanation of this below.
代码
因为我的语言是c++,我将在这里使用它
// passes a pointer (called reference in java) to an integer
void call_by_value(int *p) { // :1
p = NULL;
}
// passes an integer
void call_by_value(int p) { // :2
p = 42;
}
// passes an integer by reference
void call_by_reference(int & p) { // :3
p = 42;
}
// this is the java style of passing references. NULL is called "null" there.
void call_by_value_special(int *p) { // :4
*p = 10; // changes what p points to ("what p references" in java)
// only changes the value of the parameter, but *not* of
// the argument passed by the caller. thus it's pass-by-value:
p = NULL;
}
int main() {
int value = 10;
int * pointer = &value;
call_by_value(pointer); // :1
assert(pointer == &value); // pointer was copied
call_by_value(value); // :2
assert(value == 10); // value was copied
call_by_reference(value); // :3
assert(value == 42); // value was passed by reference
call_by_value_special(pointer); // :4
// pointer was copied but what pointer references was changed.
assert(value == 10 && pointer == &value);
}
Java中的一个例子不会有什么坏处:
class Example {
int value = 0;
// similar to :4 case in the c++ example
static void accept_reference(Example e) { // :1
e.value++; // will change the referenced object
e = null; // will only change the parameter
}
// similar to the :2 case in the c++ example
static void accept_primitive(int v) { // :2
v++; // will only change the parameter
}
public static void main(String... args) {
int value = 0;
Example ref = new Example(); // reference
// note what we pass is the reference, not the object. we can't
// pass objects. The reference is copied (pass-by-value).
accept_reference(ref); // :1
assert ref != null && ref.value == 1;
// the primitive int variable is copied
accept_primitive(value); // :2
assert value == 0;
}
}
维基百科
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference
这家伙说得很准:
http://javadude.com/articles/passbyvalue.htm
最简单的方法是在Excel文件中获取。举个例子,有两个数字,5和2分别在A1和B1单元格中,你想在第三个单元格中求出它们的和,假设是A2。
有两种方法可以做到这一点。
通过在单元格中输入= 5 + 2将它们的值传递给单元格A2。在这种情况下,如果单元格A1或B1的值发生变化,则A2中的总和保持不变。 或者通过输入= A1 + B1将单元格A1和B1的“引用”传递给单元格A2。在这种情况下,如果单元格A1或B1的值发生变化,A2中的总和也会发生变化。