Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Call by Reference

In call by reference, the address of actual argument is sent from caller function to called function and not the duplicate copy.
Formal parameters of the called function receive the address of actual arguments using pointers.
If called function makes some operation using formal parameters (address of actual arguments), actual arguments present in the caller function get affected.
For better understanding, look at the example given below.

C Programming Language Call by Reference Example Step 1


C Programming Language Call by Reference Example Step 2

Here original copy got affected.

Example

#include <stdio.h>
void calledFunction(int *a, int *b);
int main()
{
    int i=5,j=10;
    printf("\n Before sending i & j");
    printf("\n i = %d, j = %d",i,j);
    calledFunction(&i,&j);
    printf("\n");
    printf("\n After sending i & j");
    printf("\n i = %d, j = %d",i,j);
    return 0;
}

void calledFunction(int *a, int *b)
{
    *a = (*a)*2;
    *b = (*b)*2;
}

Output

Before sending i & j
i = 5, j = 10

After sending i & j
i = 10, j = 20

For the better understanding, let’s look at the diagram shown below:

C Programming Language Call by Reference Working