Pages

100 Contemporary Living Room Designs

Wednesday, August 8, 2012

PASS BY REFERENCE

Program to swap two numbers using pass by reference

#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y);  // function declaration
void main()
{
  int a,b;
  clrscr();
  cout<<"Enter two numbers a & b"<<endl;
  cin>>a>>b;
  cout<<"\nBefore swapping:\n";
  cout<<"a="<<a;
  cout<<"\nb="<<b;
  swap(a,b);     //function call
  cout<<"\nAfter swapping:\n";
  cout<<"a="<<a;
  cout<<"\nb="<<b;
  getch();
}
void swap(int &x,int &y)  //  arguments by reference
{
  int t;
  t=x;
  x=y;
  y=t;
}


OUTPUT



No comments:

Post a Comment