Advertisement
varungurnaney

HSC: Swapping with reference

Feb 7th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream.h>
  2. #include<conio.h>
  3.  
  4.  
  5. // function declaration
  6. void swap(int &x, int &y);
  7.  
  8. int main ()
  9. {
  10.     clrscr();
  11.    int a = 100;
  12.    int b = 200;
  13.  
  14.    cout << "Before swap, value of a :" << a << endl;
  15.    cout << "Before swap, value of b :" << b << endl;
  16.  
  17.    /* calling a function to swap the values using variable reference.*/
  18.    swap(a, b);
  19.  
  20.    cout << "After swap, value of a :" << a << endl;
  21.    cout << "After swap, value of b :" << b << endl;
  22.   getch();
  23.    return 0;   
  24.  
  25. }
  26.  
  27.  
  28. void swap(int &x, int &y)
  29. {
  30.    int temp;
  31.    temp = x; /* save the value at address x */
  32.    x = y;    /* put y into x */
  33.    y = temp; /* put x into y */
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement