Sunday, January 26, 2014

C++ Program to Swap Values of Two Variables Using Call by Value and Call by Reference Method

with 0 Comment
C++ Program to Swap Values of Two Variables Using Call by Value Method:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
}
void main()
{
int x=13,y=3;
clrscr();
cout<<"\nValues before invoking swap"<<x<<"\t"<<y;
swap(x,y);
cout<<"\nValues after invoking swap"<<x<<"\t"<<y;
getch();
}

C++ Program to Swap Values of Two Variables Using Call by Reference Method:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}void main()
{
int x=13,y=2;
clrscr();
cout<<"\nValues before invoking swap"<<x<<"\t"<<y;
swap(x,y);
cout<<"\nValues after invoking Swap"<<x<<"\t"<<y;
getch();

}

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive