Parameter passing mechanism in C++, Call By Value, Call By Address, Call By Reference explain with examples
Parameter passing mechanisms in C++:-
The parameters can be passed in 3 ways
in C++. They are
1) Call by value 2) Call by
address 3) Call by reference
Call by value: -
·In C++ by default a function
call passes parameters by value
·In this mechanism the values
of actual parameters will be passed to a separate set of variables known as
formal parameters
·The formal parameters of type
value parameters
·Any changes made to formal
parameters will not affect the corresponding actual parameters.
·Only one value to be return
from function
e.g void main ()
{ void
swap (int, int); // function prototype
int a=10,b=50;
swap(a,b);
// function call as call by value
cout<<a<<b; //
10 50
}
void swap (int x, int y) // function definition.
{ int
t; t=x; x=y; y=t;
cout<<x<<y; // output 50 10
}
Call by address:-
In C++ it is possible to call a function by
passing address this technique is known as call by address.
·
In call by address technique the formal parameters
must be a pointer type and they receive the addresses of actual parameters.
·
Any changes to the formal parameters are
automatically reflected back to the actual parameters in the main program.
·
It is possible to make a
function to return more than one value to the calling program
·
The address can be generated by using address
operator &.
e.g void main ( )
{ void
swap (int * , int * ); // prototype
int a,b;
a=50; b=30;
swap(&a,&b); /*call by address*/
cout
<<a<<b<<endl;
}
void swap (int *p1, int *p2) / *Receiving the addresses in
pointers*/
{ int
t; t=*p1; *p2= *p1; *p2=t; }
From the above example, it is
clear that the main() function can display the swapped values of a and b.
Call by Reference:-
·
The formal parameters must be of type reference
·
When we pass parameters by reference the formal parameters
becomes like an alias variable to the formal parameters
·
To pass arguments by reference, the function call is similar
to that of call by value. Ex swap(a,b)
·
In the function decelerator the formal parameters are
preceded by the ‘&’ operator.
·
It is possible to return more than one value from a function
to the main program
·
Any changes to the formal parameters are
automatically reflected back to the actual parameters in the main program.
·
Using this method no duplicate set
of variables are crated.
Hence the complete
program of swapping 2numbers using call by reference can be shown as follows.
void main ( )
{ void
swap (int &x, int & y);
int a,b;
a=50; b=30;
swap(a,b);
// function calling.
cout<<a<<b<<endl; }
void swap(int &x, int &y)
{ int t; t=x; x=y; y=t; }
Example: Square of two
numbers using call by value, call by address and call by reference.
(i) call bye value:
void main( )
{
void square ( int , int );
// function prototype
int a,b;
cout<<”enter value of a
and b”;
cin>>a>>b;
square(a,b); // function call, a
and b are actual arguments.
}
void squre (int c, int d)
// function definition , c and d
are formal arguments.
{
cout<<”square
is=”<<c*c<<d*d;
}
(ii) call bye address:
void main( )
{
void square ( int *, int *); // function prototype
int a,b;
cout<<”enter value of a
and b”;
cin>>a>>b;
square(&a, &b);
// function call, a and b are actual arguments.
cout<<a<<b // square values
}
void squre (int *p1, int *p2)
{
*p1=(*p1) * (*p1) ; *p2=(*p2) * (*p2) ; }
(iii) call by
reference:
void main( )
{
void square ( int & , int &); // function prototype
int a,b;
cout<<”enter value of a
and b”;
cin>>a>>b;
square(a, b); // function call, a
and b are actual arguments.
cout<<a<<b // square values
}
void squre (int &x, int
&y) //
function definition , x and y are reference variable.
{
x= x*x ; y=y*y
; }