Function Call by Reference in C
Function Call by Reference in C
Let us start this chapter with a brief overview on "pointers" and the "address
operator (&)". It is important that you learn these two concepts in order to fully
understand the mechanism of Call by Reference.
To fetch the address at which the variable has been created, we use the address
(&) operator.
Example
#include <stdio.h>
int main(){
int x = 10;
Output
x: 10 Address of x: -1990957196
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 1/7
6/16/24, 12:28 PM Function Call by Reference in C
What is a Pointer in C?
A pointer is a variable that stores the address of another variable. To declare a
pointer variable, its name is prefixed with the * symbol. The type of the pointer
variable and its host variable must be same.
The address is assigned with the & operator. The dereference operator (*) is used
with the pointer. It fetches the value of a variable whose address is assigned to the
pointer.
Example
#include <stdio.h>
int main(){
int x = 10;
int *y = &x;
Output
x: 10 Address of x: -1742755108
Address of y: -1742755104
Value at address in y: 10
Let us define the add() function that receives the references of two variables −
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 2/7
6/16/24, 12:28 PM Function Call by Reference in C
int z = *x + *y;
return z;
}
When such a function is called, we pass the address of the actual argument.
Example
Let us call the add() function by reference from inside the main() function −
#include <stdio.h>
/* function declaration */
int add(int *, int *);
int main(){
int z = *x + *y;
return z;
}
Output
When you run this code, it will produce the following output −
Addition: 30
Now let's understand how this code actually works. The main() function passes the
address of a and b to the add() function. The addresses of a and b are assigned to
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 3/7
6/16/24, 12:28 PM Function Call by Reference in C
Now focus on the statement "z = *x + *y;" inside the add() function. Remember
that x stores the address of a. The dereference operator in *x and *y fetches the
values of a and b respectively, hence z is the addition of a and b in the main()
function.
Let us understand in more detail how the Call by Reference mechanism works, with
the help of the following example that interchanges value of two variables.
#include <stdio.h>
int z;
return 0;
}
int main(){
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 4/7
6/16/24, 12:28 PM Function Call by Reference in C
return 0;
}
Output
When you run this code, it will produce the following output −
Explanation
Assume that the variables a and b in the main() function are allotted locations with
the memory address 100 and 200 respectively. As their addresses are passed to x
and y (remember that they are pointers), the variables x, y and z in the swap()
function are created at addresses 1000, 2000 and 3000 respectively.
Since "x" and "y" store the address of "a" and "b", "x" becomes 100 and "y"
becomes 200, as the above figure shows.
Inside the swap() function, the first statement "z = *x" causes the value at address
in "x" to be stored in "x" (which is 10). Similarly, in the statement "*x = *y;", the
value at the address in "y" (which is 20) is stored in the location whose pointer is
"x".
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 5/7
6/16/24, 12:28 PM Function Call by Reference in C
Finally, the statement "*y = z;" assigns the "z" to the variable pointed to by "y",
which is "b" in the main() function. The values of "a" and "b" now get swapped.
A function in C can have more than one arguments, but can return only one value.
The Call by Reference mechanism is a good solution to overcome this restriction.
Example
In this example, the calculate() function receives an integer argument by value, and
two pointers where its square and cube are stored.
#include <stdio.h>
#include <math.h>
/* function declaration */
int calculate(int, int *, int *);
int main(){
int a = 10;
int b, c;
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 6/7
6/16/24, 12:28 PM Function Call by Reference in C
*y = pow(x,2);
*z = pow(x, 3);
return 0;
}
Output
When you run this code, it will produce the following output −
a: 10
Square of a: 100
Cube of a: 1000
The Call by Reference mechanism is widely used when a function needs to perform
memory-level manipulations such as controlling the peripheral devices, performing
dynamic allocation, etc.
https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm 7/7