Function call value in C program.
Hi Everyone,
Today we will discuss about Function call value in C Program and it’s benefits. In Previous blog we discussed about function call by reference and overview of Function in C program. So before starting Function call value please see my below blogs for your reference.
https://dheeraj60.blogspot.com/2020/06/call-by-reference-function-uses-and.html
Let’s Start with Function call value : The Function call by value is a method of passing arguments to a function which copy the actual value of an argument into the formal parameter of the function. Changes made to the parameter inside the function have no effect on the argument. In C Program by default it uses call by value to pass arguments. In other word, it means the code within a function cannot alter the arguments used to call the function.
Let’s understand the Actual parameters and Formal parameter to get clear picture on function call value as we use both the parameters in program
Actual parameters that appear in function calls whereas Formal parameters that appears in function declarations.
Understand the Function call value with both the parameter with one simple C Program.
#include <stdio.h>
#include <stdlib.h>
int dh(int i)
{
i = i+1;
return i;
}
int main()
{
int a=100;
int b = dh(a);
printf("\nValue of a is: %d", a);
printf("\nValue of b is: %d", b);
return 0;
}
Here we use function dh and increment the variable by using function , We passed the variable a while calling the method, but since we are calling the function using call by value method, only the value of a is copied to the formal parameter i , so changes made to the i doesn’t reflect in the variable a. But b will be incremented by passing function by value . So output of a will be the assigned value only that is 100 and value of b is 101.
Below is the output of above program in Compiler code block.
Let’s understand the same swap program which we used in function call by reference by using Function call value.
Below is the swap program by using function call value.
#include <stdio.h>
#include <stdlib.h>
void swap( int a, int b )
{
int result ;
result = a ;
a = b ;
b = result ;
}
int main( )
{
int i = 500, j = 1000 ;
printf("\nBefore swapping the value of i and j is: %d, %d", i, j);
swap(i, j);
printf("\n After swapping value of i and j is: %d, %d", i, j);
}
Let’s understand this program here we use function swap with integer type variable and in first part of code result =a; is simply Copying a value into temporary variable result and second a=b copying the value b into and copying temporary variable result into b by using b=result . And finally for swapping we call swap function by using swap(i, j); and we assigned the value of i and j in the program.So by using swap function we can the see the value of variable i and j both before swap which we assigned and swap value by simply using swap function.
Below is the output of above program in compiler code block.
Now we can able to see assigned value of variable i and j and also swap value of i and j by using swap function. Function is called by value for i and j. So actually variable a and variable b gets swapped not variable i and j. As in call by value actual parameters are just copied into the formal parameters. So in this program it shows that there are no changes in the values, though they had been changed inside the function.
Hope you are able to understand the Function call value in program and also see the differences we used in function call by references for the same swap program we used in previous blog. Please read this blog carefully and try to write some other programs as well and let me know if you have any doubts. In next Blog i will explain Array in Function which is very important topic and we can write some interesting program by using array in Function.
Thanks.
Comments
Post a Comment