Function Call by Value in C
Function Call by Value in C
Function Call by Value in C
Formal arguments − A function needs certain data to perform its desired process.
When a function is defined, it is assumed that the data values will be provided in the
form of parameter or argument list inside the parenthesis in front of the function
name. These arguments are the variables of a certain data type.
Here, the argument variables are called the formal arguments. Inside the
function’s scope, these variables act as its local variables.
int z = x + y;
return z;
The arguments x and y in this function definition are the formal arguments.
https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm 1/5
6/16/24, 12:27 PM Function Call by Value in C
If the add() function is called, as shown in the code below, then the variables inside
the parenthesis (a and b) are the actual arguments. They are passed to the function.
#include <stdio.h>
int z = x + y;
return z;
}
int main(){
Output
When you run this code, it will produce the following output −
Addition: 30
The Call by Value method implies that the values of the actual arguments are copied
in the formal argument variables. Hence, "x" takes the value of "a" and "b" is
assigned to "y". The local variable "z" inside the add() function stores the addition
value. In the main() function, the value returned by the add() function is assigned to
"c", which is printed.
https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm 2/5
6/16/24, 12:27 PM Function Call by Value in C
Let us assume that the variables a, b and c in the main() function occupy the
memory locations 100, 200 and 300 respectively. When the add() function is called
with a and b as actual arguments, their values are stored in x and y respectively.
The variables x, y, and z are the local variables of the add() function. In the
memory, they will be assigned some random location. Let's assume that they are
created in memory address 1000, 2000 and 3000, respectively.
Since the function is called by copying the value of the actual arguments to their
corresponding formal argument variables, the locations 1000 and 2000 which are the
address of x and y will hold 10 and 20, respectively. The compiler assigns their
addition to the third local variable z which is returned.
As the control comes back to the main() function, the returned data is assigned to c,
which is displayed as the output of the program.
Example
#include <stdio.h>
/* function declaration */
void swap(int x, int y);
https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm 3/5
6/16/24, 12:27 PM Function Call by Value in C
int main(){
return 0;
}
int temp;
return;
}
Output
When you run this code, it will produce the following output −
It shows that there are no changes in the values, although they had been changed
inside the function.
https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm 4/5
6/16/24, 12:27 PM Function Call by Value in C
Since the values are copied in different local variables of another function, any
manipulation doesn’t have any effect on the actual argument variables in the calling
function.
However, the Call by Value method is less efficient when we need to pass large
objects such as an array or a file to another function. Also, in some cases, we may
need the actual arguments to be manipulated by another function. In such cases,
the Call by Value mechanism is not useful. We have to explore the Call by Reference
mechanism for that purpose. Refer the next chapter for a detailed explanation on the
Call by Reference mechanism of C.
The Call by Reference approach involves passing the address of the variable holding
the value of the actual argument. You can devise a calling method that is a mix of
Call by Value and Call by Reference. In this case, some arguments are passed by
value and others by reference.
https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm 5/5