In this article Call by Value in C we give the information about In Call by Value method, the value of actual parameters is copied into the formal parameters of the function.
Parameters in the function:
Before learning Call By Value and Call By Reference, understand the Parameters.
There are two types of parameters in the function.
1.Formal Parameter
2.Actual Parameter
Formal parameters
The parameter which is written in the declaration and definition of the function is called Formal Parameter.
for eg.
void interchange(int c, int d); // Formal Parameters
int main()
{
int i=12; j=100
interchange (i, j)
}
void interchange(int c, int d) //Formal Parameters
{
————
————
}
Actual Parameter:
The parameter which is written in the function call is called Actual Parameter.
for eg.
void interchange(int c, int d);
int main()
{
int i=12; j=100
interchange (i, j) //Actual Parameter
}
void interchange(int c, int d)
{
————
————
}
Call by Value & Call by Reference in C
In C programming language, function can be called in two ways. First call by value and second call by reference.
Call By Value
In Call by Value method, the value of actual parameters is copied into the formal parameters of the function.
The other words, “In this the value of the variable is used in the function call.”
In Call by value method, we cannot change the value of actual parameter by formal parameters.
In this, actual and formal parameters are stored in different memory locations.
Actual parameter is an argument that is used in function call while formal parameter is an argument that is used in function definition.
Its program is given below:-
#include<stdio.h>
#include<conio.h>
void interchange(int c, int d)
{
int temp;
temp = c;
c = d;
d = temp;
}
void main()
{
int i = 50, j = 70;
clrscr();
interchange (i, j); // passing value to function
printf(“\n Value of i: %d”,i);
printf(“\n Value of j: %d”,j);
getch();
}
O/P: –
Value of i: 50
Value of j: 70
Call By Reference:
In Call by reference, the address of an argument is copied into formal parameters.
T other words, “In this the address of the variable is passed to the function call like an actual parameter.”
In this, if we change the value of formal parameter then the value of actual parameter will also change.
In call by reference, both actual and formal parameters are stored in the same memory location.
Example:
#include<stdio.h>
#include<conio.h>
void interchange(int *c, int *d)
{
int temp;
temp = *c;
*c = *d;
*d = temp;
}
void main()
{
int i = 50, j = 70;
clrscr();
interchange (&i, &j); // passing value to function
printf(“\nValue of i: %d”,i);
printf(“\nValue of j: %d”,j);
getch();
}
Output –
Value of i: 70
Value of j: 50
Difference between Call by Value and Call by Reference
Call By Value
While calling the function, when we pass the value of the variable, then such functions are called call by value.
In this, if we change the copy of the variable, then the original value of the variable does not change.
This we cannot change the value of the actual variable through function call.
In this, the values of the variables are passed through a simple technique.
Call By Reference
While calling the function, when we pass the address of the variable, then such functions are called call by reference.
Change the value of the variable in it, then the value of the variable outside the function will also change.
In this we can change the value of the actual variable through function call.
This, pointer variable is required to store the address of variables.
In this, actual and formal parameters are stored in the same memory location.
It is mainly supported by Java.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining