Call by reference, value and name

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

So, I have the following pseudo code:

foo(a, b, c) { b =b++; a = a++; c = a + b*10 } X=1; Y=2; Z=3; foo(X, Y+2, Z); 

What are X, Y, and Z after calling foo if a, b, and c are all invoked by reference? if a, b and c are callsigns / result? if a, b and c is a call by name?

Another scenario:

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

I'm trying to start work on the upcoming finals, and this seemed like a good challenge to review. The passage by name is by far the most alien to me.

+7
source share
5 answers

When you pass a parameter by value, it simply copies the value inside the function parameter, and everything that is done with this variable inside the function does not reflect the original variable, for example.

 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 everything that we do with the variables inside the function is actually executed in the original memory location, for example.

 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 

to pass by name; Pass-by-name

+5
source

Call by value : the usual way ... the values ​​of the actual parameters are copied to the formal parameters.

Call by reference : instead of parameters, their addresses are transferred, and formal parameters indicate the actual parameters.

Call by name : like macros, defining an entire function replaces a function call, and formal parameters are just another name for the actual parameters.

+2
source

By value - there are no changes in the function. all your actions disappear when the function is completed.

By reference - your actions really change the variables. By name - I never heard ...

The transmission x + 1 does not change, it simply indicates functions 3 instead of 2, etc ...

+1
source

This will not change the value of X, Y, or Z if it is a missing value. When you use a function like "foo ()", it basically copies the variables (x, y and z) to other variables (a, b and c) and performs certain actions with them without changing the originals (x, y and z). For you to change the value, you would need to return the value, for example:

 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 will be the same, but z will be (x + 1) + (y + 1) * 10, which in this case will be 32.

0
source

in javascript:

  • a primitive type variable, like a string, a number always passes as a pass by value.
  • The array and object are passed as a pass by reference or passed by value based on these conditions.

    • if you change the value of this Object or array using a new object or array, then it is passed by value.

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

    here you assign a new object or array. You do not change the property value of the old object. It is also a pass by value.

    • if you change the property value of an object or array, then it is passed by reference.

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

    here you change the property value of the old object. You do not assign a new object or array to old one.so it follows the link.

the 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 
0
source

All Articles