If you are learning or learning a C programming language, then you must have read the function.
In C language, there are two ways to pass the data in a function, for passing parameters, call by value, and call by reference.
With the help of which we can pass data to the function.
Call by reference And Call by Value
So in this article, we will know what is call by value and call by reference and how does it go to pass data to this function.
Call By Value
In the call by value method, the variable's value function is given as a parameter. The call by value method is separately allocated to both the formal and actual parameters as the value of the parameter is copied to the formal parameter.
The value of the actual parameter cannot be modified by the formal parameter during this method.
The actual parameter is the argument used in the function call while the formal parameter is the argument used in the function definition.
#include <stdio.h>
void change (int n)
{
printf ("add value in function n =% d", n); // Before
n = n + 100;
printf ("add value in function n =% d", n); // After
}
int main () {
int y = 10;
Printf(“Function is : n=%d”,y)
//before
change (y); // passing value in function
Printf(“Function is : n=%d”,y
// After
return 0;
}
Output
function call y = 10 // Before
adding value in function n = 10 // Before
adding value in function n = 20 // After
function call y= 10 // After
Call by Value Example
#include <stdio.h>
void swaping (int, int);
int main ()
{
int x = 30;
int y = 50;
printf ("swapping main x =% d, b =% d", x, y); // Before
swaping (x, y);
printf ("swapping main x =% d, y =% d", x, y); } // After
void swap (int x, int y)
{
int temporally;
temporally = x;
x = y;
y = temporally;
printf ("After swap values ??function x =% d, y =% d \ n", x, y); // Formal parameters, a = 20, b = 10
}
Output
swapping the values ??in main x = 30, y = 20 //
swapping values ??in function x = 20, y = 30 // After
swapping values ??in main x = 30, b = 20 // After
Call by reference
In the call by reference method, the function is given as an address parameter of the variable. By this, the value of the actual parameter can be modified by a formal parameter. In this, the same memory is used for both the actual and formal parameters because both these parameters are stored at the address itself.
#include <stdio.h>
void change (int * n) {
printf ("Add value in function n =% d", * n); // Before
(* n) + = 10;
printf ("add value in function n =% d", * n); // After
}
int main () {
int z = 10;
printf (" Call function z =% d ", z); // Before
change (& z); // passing reference in function
Printf(“Function is : z=%d”,z)
return 0;
}
Output
Before function call z = 10
Before adding value in function n = 10
After adding value in function n = 20
After function call z = 20
Call by reference example
#include <stdio.h>
void swaping (int *, int *);
int main ()
{
int x = 100;
int y = 200;
printf ("swapping the values ??in main x =% d, y =% d", x, y); // Before
swaping (& x, & y);
printf ("swapping values ??in main x =% d, y =% d", x, y); // After
}
void swapping (int * x, int * y)
{
int temporally;
temporally = * x;
* x = * y;
* y = temporally;
printf ("After swapping values ??in function x =% d, y =% d \ n", * x, * y); // Formal parameters, x = 200, y = 100
}
Output
Before swapping the values ??in main x = 100, y = 200
After swapping values ??in function x = 200, y = 100
After swapping values ??in main x = 200, y = 100
Difference Between Call by reference And Call by Value
Call by Value
- Passes a copy of the value to the function
- In this, there is no change in the value of the actual parameter when the value of the formal parameter is changed.
- In this, a separate memory location is created for both formal parameters and actual parameters.
- An actual argument can be constant or variable
Call by reference
- The address of the variable is passed to the function
- In this, the value of the actual parameter also changes after making any changes in the formal parameter.
- Its formal and actual arguments are created at the same memory location.
- The actual argument is only variable
0 Comments