Saturday, 28 December 2013

C++ Program For Swap Value Using Call By Reference

#include<iostream.h>
swap(int *,int *);
void main()
{
  int x=40, y=20;
  cout<<“Before calling swap:”<<x<<y;
  swap(&x,&y);
  cout<< “After calling swap :”<<x<<y;
}
swap(int  *a , int  *b)
{
  int  temp;
  temp=*a;
  *a=*b;
  *b=temp;
  cout<< “with swap function”<<a<<b;

}
///////////////////////////////////////////////////////////
Call by Reference
>      In the call by reference method the address of arguments are passed as actual parameters to the called function, the function can change the contents stored at those addresses and these changes will be visible in the calling function.
>      In this when the function is working with its own arguments(formal),it is actually working on the  original data.

No comments:

Post a Comment