In this Page, we will pass the actual parameter in three-way using parameter passing method in C++ which are following,
call by value in C++
In this, the values of the parameters of the calling function are copied into the variables declared in the parameters of the function definition i.e. the value of the actual parameter is copied to the formal parameter.
The function definition uses only the values of the actual parameters (original value) copied by the formal parameters.
In a way, the function definition has duplicate values. Therefore, even if the names of both the parameters are same, the values of the actual parameter do not change even if the values of the formal parameter change.
void value(int,int); // function declaration
.................
.................
value(x,y); // call by reference
void value (int x, int y) // function definition
{
............
............
}
Here is the program,
#include<iostream>
using namespace std;
void value(int,int); // function declaration
int main()
{
int x,y; // variable declaration
cout<<"Enter two number: ";
cin>>x>>y;
cout<<"\nFirst number : "<<x;
cout<<"\nSecond number: "<<y;
value(x,y); // call by value
cout<<"\nafter call by value";
cout<<"\nFirst number : "<<x;
cout<<"\nSecond number: "<<y;
return 0;
}
void value (int x, int y) // function definition
{
int z;
z = x;
x = y;
y = z;
}
OUTPUT
Enter two number: 2 3
First number : 2
Second number: 3
after call by value
First number : 2
Second number: 3
call by reference in C++
In it, there are reference variables in the formal parameters that mean different variables name. Like the call by value, the values of actual parameters are copied into formal parameters.
When there are changes in the values of the actual parameters, then the values of the actual parameters also change.
void value(int&,int&); // function declaration
.................
.................
value(x,y); // call by reference
void value (int &a, int &b) // function definition
{
............
............
}
Here is Program,
#include<iostream>
using namespace std;
int main()
{
int x,y; // variable declaration
void value(int&,int&); // function declaration
cout<<"Enter two number: ";
cin>>x>>y;
cout<<"\nFirst number : "<<x;
cout<<"\nSecond number: "<<y;
value(x,y); // call by reference
cout<<"\nafter call by reference";
cout<<"\nFirst number : "<<x;
cout<<"\nSecond number: "<<y;
return 0;
}
void value (int &a, int &b) // function definition
{
int c;
c = a;
a = b;
b = c;
}
OUTPUT
Enter two number: 2 3
First number : 2
Second number: 3
after call by reference
First number : 3
Second number: 2
call by pointer in C++
This method of parameter passing is different from the other two. To understand this, it is necessary to understand pointer. In this method, the memory address of values in the function definition is pass,
In this also, when there are changes in values of formal parameters as well as call by reference, values of actual parameters are also changed.
void value(int*,int*); // function declaration
.................
.................
value(&x,&y); // call by reference
void value (int *a, int *b) // function definition
{
............
............
}
Here is the Program,
#include<iostream.h>
using namespace std;
int main()
{
int x,y; // variable declaration
void value(int*,int*); // function declaration
cout<<"Enter two number: ";
cin>>x>>y;
cout<<"\nFirst number : "<<x;
cout<<"\nSecond number: "<<y;
value(&x,&y); // call by pointer
cout<<"\nafter call by pointer";
cout<<"\nFirst number : "<<x;
cout<<"\nSecond number: "<<y;
return 0;
}
void value (int *a, int *b) // function definition
{
int c;
c = *a;
*a = *b;
*b = c;
}
OUTPUT
Enter two number: 2 3
First number : 2
Second number: 3
after call by pointer
First number : 3
Second number: 2
Thus we can use Parameter passing method in C++ to pass actual parameter’s value to a function definition Remember parameter passing method is the process between to pass value/data actual parameter to formal parameter.
Things to know
The only difference between call by value and call by reference is that where the parameter name (actual, formal) in the call by value is the same, the parameter name in the call by reference is different.
Previous- Function and their type | Parameter type
Next- Memory allocation in C++