Call by Reference in C: Usage and Examples
Table of Content:
In a computer language, there are two ways that arguments can be passed to a subroutine. The first is call by value. This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to the parameter have no effect on the argument.
Call by reference is the second way of passing arguments to a subroutine or function. In this process, the address(reference) of an argument(variable) is copied into the parameter. Inside the subroutine or function, the address(reference) is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument(variable).
Call by reference in C
In call by reference, original value is modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.
Note: To understand the call by reference, you must have the basic knowledge of pointers. click here to understand Pointer
Example Program
#include/* function declaration */ void swap(int *, int *); int main () { /* local variable definition */ int a = 15; int b = 20; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); /* calling a function to swap the values */ swap(&a, &b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; } /* function definition to swap the values */ void swap(int *a, int *b) { int temp; temp = *a; /* save the value of a */ *a = *b; /* put b into a */ *b = temp; /* put temp into b */ return; }
Output
Before swap, value of a : 15 Before swap, value of b : 20 After swap, value of a : 20 After swap, value of b : 15 Press any key to continue . . .
Call by Value in C
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Such function calls are called calls by value. By this what we mean is, on calling a function we are passing values of variables to it.
By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function. Consider the following program:
Example Program
#includeint sqr(int p); int main(void) { int n=10; printf("Sqrt of %d is %d\n",n, sqr(n)); return 0; } int sqr(int p) { p = p*p; return(p); }
Output
Sqrt of 10 is 100 Press any key to continue . . .
In this example, the value of the argument to sqr( ), 10, is copied into the parameter p. When the assignment p = p*p takes place, only the local variable p is modified. The variable n, used to call sqr( ), still has the value 10. Hence, the output is Sqrt of 10 is 100.
Remember that it is a copy of the value of the argument that is passed into a function. What occurs inside the function has no effect on the variable used in the call.
Remember that code within a function cannot alter the arguments used to call the function. i.e In call by value, original value is not modified.
Another example of Call by Value
Example Program
#include/* function declaration */ void swap(int p, int q); int main () { /* local variable definition */ int a = 15; int b = 20; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); /* calling a function to swap the values */ swap(a, b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; } /* function definition to swap the values */ void swap(int p, int q) { int temp; temp = p; /* save the value of p */ p = q; /* put q into p */ q = temp; /* put temp into q */ return; }
Output
Let us put the above code in a single C file, compile and execute it, it will produce the following result
Before swap, value of a : 15 Before swap, value of b : 20 After swap, value of a : 15 After swap, value of b : 20 Press any key to continue . . .
It shows that there are no changes in the values, though they had been changed inside the function.
Difference between call by value and call by reference in c
No. | Call by value | Call by reference |
---|---|---|
1 | A copy of value is passed to the function | An address of value is passed to the function |
2 | Changes made inside the function is not reflected on other functions | Changes made inside the function is reflected outside the function also |
3 | Actual and formal arguments will be created in different memory location | Actual and formal arguments will be created in same memory location |