Call by reference, value, and name

爷,独闯天下 提交于 2019-11-28 07:27:48

问题


I'm trying to understand the conceptual difference between call by reference, value, and name.

So I have the following pseudocode:

foo(a, b, c)
{
   b =b++;
   a = a++;
   c = a + b*10
}

X=1;
Y=2;
Z=3;
foo(X, Y+2, Z);

What's X, Y, and Z after the foo call if a, b, and c are all call by reference? if a, b, and c are call-by-value/result? if a, b, and c are call-by-name?

Another scenario:

X=1;
Y=2;
Z=3;
foo(X, Y+2, X);

I'm trying to get a head start on studying for an upcoming final and this seemed like a good review problem to go over. Pass-by-name is definitely the most foreign to me.


回答1:


When you pass a parameter by value, it just copies the value within the function parameter and whatever is done with that variable within the function doesn't reflect the original variable e.g.

foo(a, b, c)
{
   b =b++;
   a = a++;
   c = a + b*10
}

X=1;
Y=2;
Z=3;
foo(X, Y+2, Z);
//printing will print the unchanged values because variables were sent by value so any //changes made to the variables in foo doesn't affect the original.
print X; //prints 1
print Y; //prints 2
print Z; //prints 3

but when we send the parameters by reference, it copies the address of the variable which means whatever we do with the variables within the function, is actually done at the original memory location e.g.

foo(a, b, c)
{
   b =b++;
   a = a++;
   c = a + b*10
}

X=1;
Y=2;
Z=3;
foo(X, Y+2, Z);

print X; //prints 2
print Y; //prints 5
print Z; //prints 52

for the pass by name; Pass-by-name




回答2:


Call by Value : normal way... values of actual parameters are copied to formal parameters.

Call by Reference : instead of the parameters, their addresses are passed and formal parameters are pointing to the actual parameters.

Call by Name : like macros, the whole function definition replaces the function call and formal parameters are just another name for the actual parameters.




回答3:


By value - there is no changes out the function. all your actions vanish when the function finished.

By reference - your actions indeed changes the variables. By name - I've never heard ...

Passing x+1 is not change, just tells to the function 3 instead 2 or etc...




回答4:


This won't change the value of X, Y or Z if it is pass-by-value. When you use a function such as "foo()", it basically copies the variables (x, y and z) into other variables (a, b, and c) and does certain actions with them, without changing the originals (x, y and z). For you to change a value you would have to return a value, something like this:

foo(a, b, c)
{
a = a++;
b = b++;
c = a + b * 10;
return c;
}

x = 1;
y = 2;
z = 3;
z = foo(x, y+2)

Then x and y would be the same, but z would be (x+1)+(y+1)*10 which in this case would be 32.




回答5:


in javascript :

  1. primitive type variable like string,number are always pass as pass by value.
  2. Array and Object is passed as pass by reference or pass by value based on these condition.

    • if you are changing value of that Object or array with new Object or Array then it is pass by Value.

      object1 = {item: "car"}; array1=[1,2,3];

    here you are assigning new object or array.you are not changing the value of property of old object.so it is pass by value.

    • if you are changing a property value of an object or array then it is pass by Reference.

      object1.item= "car"; array1[0]=9;

    here you are changing a property value of old object.you are not assigning new object or array to old one.so it is pass by reference.

Code

    function passVar(object1, object2, number1) {

        object1.key1= "laptop";
        object2 = {
            key2: "computer"
        };
        number1 = number1 + 1;
    }

    var object1 = {
        key1: "car"
    };
    var object2 = {
        key2: "bike"
    };
    var number1 = 10;

    passVar(object1, object2, number1);
    console.log(object1.key1);
    console.log(object2.key2);
    console.log(number1);

Output: -
    laptop
    bike
    10


来源:https://stackoverflow.com/questions/15823903/call-by-reference-value-and-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!