Call by reference in C
Call by reference in C
Call by reference in C is the method when we call a function by passing the addresses of actual parameters then this way of calling the function.
In this method,the memory allocation is same for both formal parameters and actual parameters. All the operations in the function are perform on the value store at the address of the actual parameters, and the modify value gets store at the same address. And Original value is modify because we pass reference (address).
Program
#include <stdio.h> void swapnum(int* i, int* j) { int temp = *i; *i = *j; *j = temp; } int main(void) { int a = 10, b = 20; swapnum(&a, &b); printf("a is %d and b is %d\n", a, b); return 0; }
Output
a is 20 and b is 10
Advantages of using Call by reference method
- The function can change the value of the argument, which is useful.
- It does not create the same data for holding only one value which helps you to save memory space.
- This process is very fast because there is no copy of the argument made
- A person who reads the code never knows that value can be modified in the function.
Also, read the Call by value in C.