Call by Reference Function in C Program.
Hi Everyone,
Today we will discuss about call by Reference Function in C . In my previous blog I explained about overview and benefits of functions in C so before starting this topic please go through below blog so that you can easily understand Call by reference Function.
Let’s start with call by reference function in C Program. First thing we need to start the parameters to understand reference function. The parameters are Actual Parameter and Formal parameter. Actual parameter that appears in function call while parameters that appear in function declarations is known as formal parameter.
Let’s understand with one example .
Int add (int i , int j);
Here i and j are formal parameter and calling the functions as below is Actual parameter.
Int a =add(100,200);
So the call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument. In other word call by reference, the operation performed on formal parameters, affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters. Let’s understand this by one simple example of call by reference function in Program to increment the value of variable by 2.
#include <stdio.h>
#include <stdlib.h>
void dh(int *abc)
{
*abc = *abc+2;
}
int main()
{
int i=101;
dh(&i);
printf("Value of variable i is: %d", i);
return 0;
}
Here in above program we function dh and explanation of code is as below.
Here we are performing the increment on variable * abc, however the abc is a pointer that holds the address of variable i, which means the increment is actually done on the address where value of i is stored. In this way of calling the function is known as call by reference. Instead of passing the variable i, we are passing the address of variable i . So for the above program we will get the output 103 as we assigned the value of i is 101 and increment is done by using *abc = *abc+2;
Below is the output of Above code in compiler code-block .
Let’s understand with one more program by using reference method function . We use function swap by passing values by reference and get the value before and after swap in main and function value .
Below is the Program.
#include <stdio.h>
#include <stdlib.h>
void swap ( int *a, int *b )
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
int main( )
{
int i = 100 , j= 200 ;
printf("Before swapping:");
printf("\ni value is %d", i);
printf("\nj value is %d", j);
printf("\ni value is %d", i);
printf("\nj value is %d", j);
return 0;
}
Let’s understand this program here we use function swap in main we simply get the assigned value of i and j variable that is before swapping and for swap we use calling swap function that is swap( &i, &j ) and printing the After swapping value of variable i and j.
Below is the output of above program in compiler code block.
Hope you are able to understand this swap program by using swap function call by reference. Please go through this blog carefully as in next blog i will come up with another topic of function that is Function call by value. Please try to write some more programs by using function call by reference and let me know if you have any doubt. In next blog i will come up with Function call by value with programs so please read this blog carefully.
Thanks.
Comments
Post a Comment