1.

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

Answer»

Let us first take a look at the definitions of Call by Value and Call by Reference before taking a look at the differences between the two:

  • Call By Value: In this way of parameter passing, the values of actual parameters are copied to the formal parameters of the function, and the two types of parameters are stored in separate memory regions. As a result, any changes MADE inside functions are not reflected in the caller's actual arguments.
  • Call by Reference: In Call by Reference, both the actual and formal parameters refer to the identical locations and because of it, any changes performed within the function are reflected in the caller's actual parameters.

Let us now take a look at the differences between Call by Value and Call by Reference in detail:

CALL BY VALUE CALL BY REFERENCE
In Call by Value, the value of each VARIABLE in the calling function is transferred into the called function's corresponding dummy variables.In Call by Reference, the addresses of the calling function's actual variables are copied into the called function's dummy variables.
In Call by Value, changes to the dummy variables in the called function have no influence on the values of the actual variables in the calling function.Since addresses of the actual variables of the calling function are being passed in the called function, any changes made to the variables in the called function gets reflected in the variables of the calling function also.
We can't change the values of actual variables through function CALLS in call by values.In call by reference, we can change the actual values of variables through function calls.

An example each to illustrate the difference between Call by Value and Call by Reference in C is given below:

Call by Value Example:

// Illustrating call by value in C#INCLUDE <stdio.h>// Prototype of the function "addNumbers"void addNumbers(int a, int b);// The main function of the C programint main(){ int X = 50, y = 30; // Passing x and y by Values addNumbers(x, y); printf("x = %d y = %d\n", x, y); return 0;}// Function to add two Numbers and stores the result in the first number passedvoid addNumbers(int a, int b){ a = a + b; printf("a = %d b = %d\n", a, b);}

Output:

a = 80 b = 30a = 50 b = 30


NOTE: The values of the parameters passed did not change in Call By Value, that is, x remained as 50 and y remained as 30.

Call by Reference Example:

// Illustrating call by value in C#include <stdio.h>// Prototype of the function "addNumbers"void addNumbers(int *a, int *b);// The main function of the C programint main(){ int x = 50, y = 30; // Passing x and y by Values addNumbers(&x, &y); printf("x = %d y = %d\n", x, y); return 0;}// Function to add two Numbers and stores the result in the first number passedvoid addNumbers(int *a, int *b){ *a = *a + *b; printf("a = %d b = %d\n", a, b);}

Output:

a = 80 b = 30a = 80 b = 30

NOTE: The values of the parameters passed changed in Call By Reference, that is, x became 80 and y became 30.



Discussion

No Comment Found