Monday, 30 April 2012

Passing an argument using Call by Reference in C++


#include <iostream>

using namespace std;

void swap(int&, int&);

int main()
{
  int m = 3,
      n = 5;

  cout << "Before swapping: m = " << m<< " n = " << n << endl;

  swap(m, n);

  cout << "After swapping: m = " << m<< " n = " << n << endl;

  return 0;
}
void swap(int& a, int& b)
{
  int temp;

  temp = a;
  a    = b;
  b    = temp;
}

No comments:

Post a Comment