-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathq-7.cpp
62 lines (54 loc) · 1.26 KB
/
q-7.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*Write a program to demonstrate the concept of call-by-value, call-by-reference & call-by address by taking swapping of two numbers as an example.*/
#include<iostream>
using namespace std;
//Function for Call by value
void swap1(int a, int b)
{
int t;
t=a;
a=b;
b=t;
}
//Function for Call by address
void swap2(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
//Function for Call by refernce
void swap3(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
int main()
{
cout<<"Enter 2 nos."<<endl;
int x1,y1;
cin>>x1>>y1;
int x,y;
x=x1;y=y1;
cout<<"Call by value"<<endl;
cout<<"Before Swapping"<<endl;
cout<<x<<"\t"<<y<<endl;
swap1(x,y); //Calling by values
cout<<"After Swapping"<<endl;
cout<<x<<"\t"<<y<<endl;
x=x1;y=y1;
cout<<"Call by Address"<<endl;
cout<<"Before Swapping"<<endl;
cout<<x<<"\t"<<y<<endl;
swap2(&x,&y); //Calling by address
cout<<"After Swapping"<<endl;
cout<<x<<"\t"<<y<<endl;
x=x1;y=y1;
cout<<"Call by Reference"<<endl;
cout<<"Before Swapping"<<endl;
cout<<x<<"\t"<<y<<endl;
swap3(x,y); //Calling by reference
cout<<"After Swapping"<<endl;
cout<<x<<"\t"<<y<<endl;
return 0;
}