1.

Differentiate between Call by Value and Call by Reference.

Answer»

Before we look at the key differences between the two, let us take a look at the definitions of Call by Value and Call by Reference:

  • Call by Reference: In Call by Reference, both the actual and formal parameters REFER to the same location, and any changes made within the function are reflected in the caller's actual parameters as a result.
  • Call by Values: The values of actual parameters are copied to the formal parameters of the function in this method of parameter passing, and the two types of parameters are stored in distinct memory areas. As a result, any changes made inside functions are not reflected in the actual arguments passed to the caller.

Now, let us take a closer look at the distinctions between Call by Value and Call by Reference:

CALL BY VALUE CALL BY REFERENCE
The value of each variable in the calling code is transferred into the corresponding parameter variables in the called function in Call by Value.The addresses of the calling function's actual variables are copied into the called function's dummy variables in Call by Reference.
Changes to the parameter variables of the called function will have ZERO impact on the values of the variables of the calling function when using Call by Value.Because the called function receives the addresses of the calling function's actual variables, any changes made to the called function's variables are reflected in the calling function's variables as well.
In call-by-values, we can't change the values of actual variables using function calls.Through function calls, we can change the real values of variables in the call by reference.

The following is an example of each to demonstrate the difference between Call by Value and Call by Reference in C:

Coding Example of Call By Value:

// Illustration of Calling VIA Value using C program#include <stdio.h>// Prototype of the function "subtract"void subtract(float c, float d);// The main function of the C codeint32_t main(){ float a = 71.0, b = 21.0; // Passing a and b and calling the subtract function by Value subtract(a, b); printf("a = %f b = %f\n", a, b); return 0;}// Method DEFINITION to subtract two NUMBERS and store the answer in the first number passedvoid subtract(float c, float d){ c = c - d; printf("c = %f d = %f\n", c, d);}

Output:

a = 50.0 b = 21.0 // Printed value in the subtract functiona = 71.0 b = 21.0 // Printed value in the main function

NOTE: The values of the parameters passed did not change in Call By Value, that is, a remained as 71 and b remained as 21.

Coding Example of Call By Reference:

// Illustration of Calling via Reference using C program#include <stdio.h>// Prototype of the function "subtract"void subtract(float *c, float *d);// The main function of the C codeint32_t main(){ float a = 71.0, b = 21.0; // Passing a and b and calling the subtract function by Reference subtract(&a, &b); printf("a = %f b = %f\n", a, b); return 0;}// Method definition to subtract two Numbers and store the answer in the first number passedvoid subtract(float *c, float *d){ *c = *c - *d; printf("c = %f d = %f\n", c, d);}

Output:

a = 50.0 b = 21.0 // Printed value in the subtract functiona = 50.0 b = 21.0 //. Printed value in the main function

NOTE: The values of the parameters passed changed in Call By Reference, that is, a became 50 and b became 21.



Discussion

No Comment Found