-
Notifications
You must be signed in to change notification settings - Fork 0
/
PASSBY.C
93 lines (85 loc) · 2.75 KB
/
PASSBY.C
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//program to demonstrate the use of pass by value and pass by reference for parameter passing.
//program output is self-explanatory! Explains itself while producing the output :p
//header files
#include<stdio.h>
#include<conio.h>
//fucntion prototypes
void addv(int,int,int); //add using pass by Value
void addr(int*,int*,int*); //add using pass by Reference
int main()
{
int a,b,res,ch;
res = 0;
clrscr();
printf("\nPress any key to step through during execution!..");
printf("\n\nValue of res in main() before any processing: %d",res);
getche();
printf("\n\nEnter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
printf("\n\n1. Press 1 for Addition using pass by value.");
printf("\n\n2. Press 2 for Addition using pass by reference.\n\nChoice?: ");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1 :
{
printf("\n\nAddition using pass by value!\n");
printf("============================");
getche();
printf("\n\nPassing your input as value to add function...");
addv(a,b,res); //call by value
printf("\n\nBack in main!\n");
getche();
printf("\nValue of res in main(): %d\t(Unchanged)",res);
getche();
printf("\n\nConclusion: Pass by value operates on local copies of variables \nand hence does not change the actual variables in main().");
break;
}
case 2 :
{
printf("\n\nAddition using pass by reference!\n");
printf("================================");
getche();
printf("\n\nPassing your input as a reference to add function...");
addr(&a,&b,&res); //call by reference
printf("\n\nBack in main!\n");
getche();
printf("\nValue of res in main(): %d\t(Changed)",res);
getche();
printf("\n\nConclusion: Pass by ref operates directly on the addresses of the \npassed variable and hence actual/original variables in main() are changed.");
break;
}
}
getche();
return 0;
}
void addv(int a, int b, int res) //pass by Value, works on local copies inside the function
{
getche();
printf("\n\nInside add() function\n");
getche();
printf("\nCreated local copies of your arguments.\nOriginal value in main() shall remain unchanged.");
res = a + b;
getche();
printf("\n\nCalculated value of res in add function: %d",res);
getche();
printf("\n\nReturning to main()...");
getche();
}
void addr(int*a, int*b, int*res) //pass by Reference, works on actual parameter(addresses),
//copies the address of the argument being passed into the formal parameters
{
getche();
printf("\n\nInside add() function\n");
getche();
printf("\nOperating directly on addresses of arguments.\nOriginal value in main() shall change.");
*res = *a + *b;
getche();
printf("\n\nCalculated value of res in add function: %d",*res);
getche();
printf("\n\nReturning to main()...");
getche();
}