Calling Function, Return By Value, Return By Address, Return By Reference

Three Categories of Calling Function, Return By Value, Return By Address, Return By Reference explain with example and Program

There are Three categories of Calling Functions in C++


1.     Return by Value :-

1.     Here the value returned by the function after it is executed is a simple value.
2.     Example :-
int  greater(int a, int b)
{
     if ( a > b)
{    return a ; }
else
{    return b ; }
}
main()
{
    int i, j ;
    cin >> i >> j ;
    cout << ” Greater Value is:- “ << greater ( i , j );
}




2.     Return by Address : -

1.     Here the returned value is an address, and has to be received in a pointer.
2.     Example :-
int*  fill (int *p)
{
     p = new int [5] ;
    for ( int j = 0 ; j < 5 ; j ++ )
    {
        cin >> p [ j ] ;   }
    return p ;
}
main()
{
    int *q;
    q = fill ( q ) ;   
      for ( int k = 0 ; k < 5 ; k ++ )
cout<< q [ k ] ;
}



3.     Return by Reference :-

1.     Here value returned is the reference to a variable.
2.     Example :-
int&  greater(int & r, int & j)
{
    if ( r > j )
    {   return r ;    }
            else
            {  return j ;         }
}
main()
{
    int  a = 10, b = 5 ;
greater ( a , b ) = 100 ; // Here the reference to the greatest of a and b will be returned
                                      and will be assigned the value 100
cout<< a << b ; // output  100  5
}



Labels: