Call by reference, value, and name
Asked Answered
A

6

8

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.

Alfaro answered 5/4, 2013 at 0:15 Comment(4)
Hover over the tags you just put on the question, and look at the descriptions. I've never heard of pass by name, unless that's some sort of Reflection.Linkoski
Pass by Name: #838579Linkoski
Do you know what pointers are? Passing by reference is just passing a pointer while passing by value is passing the value itself.Reunion
en.wikipedia.org/wiki/Evaluation_strategyLinkoski
W
6

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

Wellbred answered 5/4, 2013 at 0:25 Comment(2)
for pass-by-reference, the passed thing must be an lvalue (i.e. be assignable). Y+2 is not an lvalue, so I am not sure what assigning to it would even mean.Intemerate
I can't find any difference in your examples of call by value and call by reference....you are just changing values of variables in comment. I want to know that how can i pass variables by value and by reference differently because all the code is same in your example. so, i can't get it.Competence
L
1

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...

Liquidity answered 5/4, 2013 at 0:27 Comment(0)
D
1

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.

Daimon answered 1/12, 2014 at 11:17 Comment(0)
A
0

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.

Apteral answered 5/4, 2013 at 0:23 Comment(0)
C
0

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
Chunk answered 28/10, 2014 at 13:49 Comment(0)
T
0

In Call by value, a copy of the variable is passed whereas in Call by reference, a variable itself is passed. In Call by value, actual and formal arguments will be created in different memory locations whereas in Call by reference, actual and formal arguments will be created in the same memory location.

Threatt answered 28/7, 2021 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.