Header Ads

C++ program to demonstrate call by value & call by reference

CALL BY VALUE

#include<iostream>
using namespace std;
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int a=100,b=200;
swap(a,b);//call by value
cout<<"value of a  :"<<a<<endl;
cout<<"value of b  :"<<b<<endl;
return 0;

}

CALL BY REFERENCE

#include<iostream>
using namespace std;
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int a=100,b=200;
swap(&a,&b);//call by reference
cout<<"value of a  :"<<a<<endl;
cout<<"value of b  :"<<b<<endl;
return 0;

}

No comments

Powered by Blogger.