Q:

C++ program to demonstrate methods of passing arguments in function:

0

C++ program to demonstrate methods of passing arguments in function:

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Methods of passing arguments in function example in C++.

/*C++ program to demonstrate methods of passing arguments in function.
  Pass by value, Pass by reference, Pass by address.
*/
 
#include  <iostream>
using namespace std;
 
void swapByValue( int a , int b  );
void swapByRef  ( int &a, int &b );
void swapByAdr  ( int *a, int *b );
 
int main()
{
    int x = 10;
    int y = 20;
 
    cout << endl;
    cout << "Value before Swapping x:" << x << " y:" << y << endl;
    swapByValue( x , y  ); /*In call by value swapping does not reflect in calling function*/
    cout << "Value After  Swapping x:" << x << " y:" << y << endl << endl; 
 
    cout << "Value before Swapping x:" << x << " y:" << y << endl;
    swapByRef( x , y  );  /*Swapping reflect but reference does not take space in memory*/
    cout << "Value After  Swapping x:" << x << " y:" << y << endl << endl; 
 
    x = 50;
    y = 100;
 
    cout << "Value before Swapping x:" << x << " y:" << y << endl;
    swapByAdr( &x , &y  ); /*Swapping reflect but pointer takes space in memory*/
    cout << "Value After  Swapping x:" << x << " y:" << y << endl << endl;  
 
    return 0;
}
 
void swapByValue( int a , int b  )
{
    int c;
     
    c = a;
    a = b;
    b = c;
}
 
void swapByRef( int &a , int &b  )
{
    int c;
     
    c = a;
    a = b;
    b = c;
}
 
void swapByAdr( int *a , int *b  )
{
    int c;
     
     c = *a;
    *a = *b;
    *b =  c;
}

Output

    Value before Swapping x:10 y:20
    Value After  Swapping x:10 y:20

    Value before Swapping x:10 y:20
    Value After  Swapping x:20 y:10

    Value before Swapping x:50 y:100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to demonstrate example of function ove... >>
<< C++ program to demonstrate example of Default Argu...