Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between call by reference and copy/restore

What's the difference in the outcome between call by reference and copy/restore?

Background: I'm currently studying distributed systems. Concerning the passing of reference parameters for remote procedure calls, the book states that: "the call by reference has been replaced by copy/restore. Although this is not always identical, it is good enough". I understand how call by reference and copy/restore work in principle, but I fail to see where a difference in the result may be?

like image 371
mort Avatar asked Jan 13 '12 09:01

mort


People also ask

What do you mean by call by reference?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What is the difference between call by reference and call by value?

In the Call by Value method, there is no modification in the original value. In the Call by Reference method, there is a modification in the original value. In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument.

What is the difference between call by reference and call by address?

Call By Address is a way of calling a function in which the address of the actual arguments is copied to the formal parameters. But, call by reference is a method of passing arguments to a function by copying the reference of an argument into the formal parameter.

What is difference between pass by value and pass by reference?

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.


2 Answers

Examples taken from here.

Main code:

#include <stdio.h>

  int a;

  int main() {
      a = 3;
      f( 4, &a );
      printf("&#37;d\n", a);
      return 0;
  }

Call by Value:

f(int x, int &y){
    // x will be 3 as passed argument
    x += a;
    // now a is added to x so x will be 6
    // but now nothing is done with x anymore
    a += 2*y;
    // a is still 3 so the result is 11
}

Value is passed in and has no effect on the value of the variable passed in.

Call by Reference:

f(int x, int &y){
    // x will be 3 as passed argument
    x += a;
    // now a is added to x so x will be 6
    // but because & is used x is the same as a
    // meaning if you change x it will change a
    a += 2*y;
    // a is now 6 so the result is 14
}

Reference is passed in. Effectively the variable in the function is the same as the one outside.

Call with Copy/Restore:

int a;
void unsafe(int x) {
    x= 2; //a is still 1
    a= 0; //a is now 0
}//function ends so the value of x is now stored in a -> value of a is now 2

int main() {
    a= 1;
    unsafe(a); //when this ends the value of a will be 2
    printf("%d\n", a); //prints 2
}

Value is passed in and has no effect on the value of the variable passed in UNTIL the end of the function, at which point the FINAL value of the function variable is stored in the passed in variable.

The basic difference between call by reference and copy/restore then is that changes made to the function variable will not show up in the passed in variable until after the end of the function while call by reference changes will be seen immediately.

like image 139
N_A Avatar answered Sep 24 '22 18:09

N_A


Call by Copy/Restore is a special case of call-by-reference where the provided reference is unique to the caller. The final result on the referenced values will not be saved until the end of the function.

This type of calling is useful when a method in RPC called by reference. The actual data is sent to the server side and the final result will send to the client. This will reduce the traffic, since the server will not update the reference each time.

like image 21
Husam Avatar answered Sep 22 '22 18:09

Husam