-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx 3A..Call by value & Call by reference.cpp
46 lines (44 loc) · 1.44 KB
/
Ex 3A..Call by value & Call by reference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<conio.h>
using namespace std;
int main(void)
{
int x,y,n;
void swap1(int,int);
void swap2(int*x,int*y);
cout<<"enter two values to be swapped\n";
cin>>x>>y;
cout<<"\nfor CALL BY VALUE press1";
cout<<"\nfor CALL BY REFERENCE press2";
cin>>n; switch(n) {
case 1: cout<<"\nCALL BY VALUE";
cout<<"\nvalues before swap()"<<x<<"\t"<<y;
swap1(x,y);
cout<<"\nafter swap outside of swap1()";
cout<<"\nx="<<x<<"\ny="<<y;
break;
case 2:
cout<<"\nCALL BY REFERENCE";
cout<<"\nvalue before swap"<<x<<y;
swap2(&x,&y);
cout<<"\nafter swap(outside of swap2)"<<x<<y;
break;
}
getch();
}
void swap1(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\nswapped values(inside swap1())"<<x<<"\t"<<y;
}
void swap2(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
cout<<"\nswapped value(inside swap2())"<<*x<<"\t"<<*y;
}