Call By Value Function In C
Function call by value is the standard way to call a function when using the C programming language. It's critical to comprehend the terminology we'll use to explain this before we examine function calls by value.
Actual Parameters: The values of parameters that are displayed in function calls are the actual parameters.
Formal Parameters: The values of parameters that are displayed in declarations are known as formal parameters.
- The variables values are passed to a function when we call it. These functions fall under the category of call by values.
- With this method, each variable's value is copied into the calling function since each variable in calling functions corresponds to a dummy variable in the called function.
- As a result, using this technique, changes made to the dummy variables in the called function won't affect the values of the real variables in the calling function.
Example:
#include <stdio.h>
int sum(int var1, int var2)
{
int var3=var1+var2;
return var3;
}
int main()
{
int a =4;
int b = 2;
int c = sum(a, b);
printf("%d", c);
return 0;
}
OUTPUT:
![Call By Value Function In C](https://static.tutorialandexample.com/c/call-by-value-function-in-c1.png)
Explanation:
Variables a and b serve as the formal parameters (or formal arguments) in the example above. The real arguments, often known as the real parameters, are variables a and b. The values may also be the actual parameters. Similar to sum (4, 2), the parameters in this case are 4 and 2.
What does "function call by value" mean?
Function call by value is the term used to describe when we actually pass the parameters when invoking a function.
In this case, the values of the formal parameters are transferred from the actual parameters. As a result, modifications made to the formal parameters are not reflected in the real parameters.
An example of calling a function by value:
Since the actual arguments in a call by value are copied to the formal arguments, as was already mentioned, any operation on the arguments has no effect on the actual parameters. To further understand this, let's look at an example:
// C Program for implementing the Call by value Using Functions
#include <stdio.h>
int increment_num(int var)
{
var = var+1;
return var;
}
int main()
{
int num_1;
printf("Enter a Number: ");
scanf("%d",&num_1);
int num_2 = increment_num(num_1);
printf("Num_1 value is: %d", num_1);
printf("\nNum_2 value is: %d", num_2);
int num_3 = num_1 + num_2;
printf("\nAddition of Num_1 and Num_2 is given by : %d",num_3);
return 0;
}
OUTPUT:
![Call By Value Function In C](https://static.tutorialandexample.com/c/call-by-value-function-in-c2.png)
Explanation:
Although we called the procedure with the variable num_1 given, only the value of num_1 was copied to the formal argument var since we called the function using the call by value technique. As a result, changes made to the var are not reflected in num_1.
Example 2: Using Function Call by Value, switching up the numbers
// C Program for implementing the Call by value Using Functions for Swapping of two Numbers
#include <stdio.h>
void swap_num( int a , int b )
{
int temp ;
/* Copying the value of "a" to a temporary variable */
temp = a ;
/* adding the value of "b" to "a" */
a = b ;
/* Moving the value of a temporary variable to "b" */
b = temp ;
}
int main( )
{
int num1 = 3, num2 = 4 ;
printf("\nBefore swapping the numbers are: %d, %d", num1, num2);
/* call to the swap function */
swap_num(num1, num2);
printf("\nAfter swapping the numbers are: %d, %d", num1, num2);
return 0;
}
OUTPUT:
![Call By Value Function In C](https://static.tutorialandexample.com/c/call-by-value-function-in-c3.png)
Explanation:
For the same reason, function is called by value for both num1 and num2. So, rather than numbers 1 and 2, a and b are switched. The formal parameters are just copies of the actual parameters, similar to a call by value.
Advantages of Call by Value Function:
Utilising the call by value approach has various benefits. According to them,
- To begin with, the original variable is not altered by the technique. As a result, data is being preserved.
- The actual contents of the actual arguments are never changed when a function is invoked, which is another important point.
- When it comes to formal arguments, genuine arguments lose their value. As a result, modifications to the formal argument have no impact on the cases themselves.