Calling method is an important phenomenon in programming. There are different calling methods in different programming language. But here we discuss about C++. Before discuss about calling method in C++ we will take one review of calling method in C. In C we can call method by using call by value and call by pointer. In C++ we can call method by using call by value and call by reference. In this post we will both method in detail and also differentiate between call by pointer and call by reference.
In traditional C call by value is used in function call. But in C++ call by reference is used.
Let we understand how call by value and call by reference by the help of swap function , Also know their working and implementation.
CALL BY VALUE
void swap ( int a , int b )
{
int temp ;
temp = a ;
a = b ;
b = temp ;
cout << a << b ;
}
void main ( )
{
int x , y ;
x = 10 ;
y = 20 ;
cout << x << y ;
swap ( x , y ) ;
cout << x << y ;
}
Output :
x = 10 , y = 20
a = 20 , b = 10
x = 10 , y = 20
Explanation :
Here in call by value method it is clear that in swap function void swap ( int a , int b ) value of a & b can be change so it known as formal arguments. But the final value of a & b is x & y in main () function swap ( x , y ) which can not change so it is known as actual argument.
Now from the above details we can say that changes takes place in formal arguments does not reflect in actual arguments.
CALL BY POINTER IN C
Output :
x = 10 , y = 20
a = 20 , b = 10
x = 10 , y = 20
Explanation :
Suppose if
int a = 5 ;
int &b = a;
In above statement &b = a it doesn't means that the value of a assign in address of b. But it mean that b is only the another or second name of a , So if a = 5 than b = 5. this is the concept of reference.
When we pass argument by reference the formal argument in called function will only change our name of actual argument in calling function. This mean that when the function is working with its own arguments it is actually working on the original data.
void swap ( int *a , int *b )
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
cout << a << b ;
}
void main ( )
{
int x , y ;
x = 10 ;
y = 20 ;
cout << x << y ;
swap ( &x , &y ) ;
cout << x << y ;
}
Explanation :
CALL BY REFERENCE IN C++Explanation :
Here in above example int *a and int *b is pointer to integer type of variable. In C & (Ampersand) is used to indicate address but in C++ it is used as reference.
void swap ( int &a , int &b )
{
int temp ;
temp = a ;
a = b ;
b = temp ;
cout << a << b ;
}
void main ( )
{
int x , y ;
x = 10 ;
y = 20 ;
cout << x << y ;
swap ( x , y ) ;
cout << x << y ;
}
Output :
x = 10 , y = 20
a = 20 , b = 10
x = 10 , y = 20
Explanation :
In C & (Ampersand) is used to indicate address but in C++ it is used as reference.
int a = 5 ;
int &b = a;
In above statement &b = a it doesn't means that the value of a assign in address of b. But it mean that b is only the another or second name of a , So if a = 5 than b = 5. this is the concept of reference.
When we pass argument by reference the formal argument in called function will only change our name of actual argument in calling function. This mean that when the function is working with its own arguments it is actually working on the original data.
No comments:
Post a Comment