Write a program to demonstrate the use of call by
value and call by reference.
#include<iostream>
using namespace std;
void value(int ,int);
void ref(int &,int &);
int main()
{
int a,b;
cout<<"Enter a & b: ";
cin>>a>>b;
value(a,b);
cout<<"in nain: ";
cout<<"\nA= "<<a<<endl<<"B= "<<b<<endl;
ref(a,b);
cout<<"in nain: ";
cout<<"\nA= "<<a<<endl<<"B= "<<b<<endl;
return 0;
}
void value(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"in fun call by value: "<<endl;
cout<<"A= "<<a<<endl<<"B= "<<b<<endl;
}
void ref(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"in fun call by reference: "<<endl;
cout<<"A= "<<a<<endl<<"B= "<<b<<endl;
}
Output :-
#include<iostream>
using namespace std;
void value(int ,int);
void ref(int &,int &);
int main()
{
int a,b;
cout<<"Enter a & b: ";
cin>>a>>b;
value(a,b);
cout<<"in nain: ";
cout<<"\nA= "<<a<<endl<<"B= "<<b<<endl;
ref(a,b);
cout<<"in nain: ";
cout<<"\nA= "<<a<<endl<<"B= "<<b<<endl;
return 0;
}
void value(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"in fun call by value: "<<endl;
cout<<"A= "<<a<<endl<<"B= "<<b<<endl;
}
void ref(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"in fun call by reference: "<<endl;
cout<<"A= "<<a<<endl<<"B= "<<b<<endl;
}
Output :-
No comments:
Post a Comment