-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass_by_reference.txt
202 lines (123 loc) · 4.39 KB
/
pass_by_reference.txt
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Section 12: Pass by Reference
------------------------------------------------------------------------------------
Pass by Value
- there are a few different ways you can pass data to a function
+ pass by value
+ pass by reference (C simulates)
- pass by value is when a function copies the actual value of an
argument into the formal parameter of the function
+ changes made to the parameter inside the function have
no effect on the argument
- C programming uses call by value to pass arguments
+ means the code within a function cannot alter the
arguments used to call the function
+ there are no changes in the values, though they had
been changed inside the function
Example: Pass by Value
/* function defintion to swap the values */
void swap(int x, int y) {
int temp;
temp = x; // save the value of x
x = y; // put y into x
y = temp; // put temp into y
}
int main(void) {
// local variable definition
int a = 100;
int b = 200;
printf("Before swap, value of a: %d\n", a);
printf("Before swap, value of b: %d\n", b);
// calling a function to swap the values
swap(a, b);
printf("After swap, value of a: %d\n", a);
printf("After swap, value of b: %d\n", b);
return 0;
}
Passing Data Using Copies of Pointers
- pointers and functions get along quite well together
+ you can pass a pointer as an argument to a function
and you can also have a function return a pointer
as its result
- pass by reference copies the address of an argument into the
formal parameter
+ the address is used to access the actual argument
used in the call
+ means the changes made to the parameter affect the
passed argument
- to pass a value by reference, argument pointers are passed
to the functions just like any other value
+ you need to declare the function parameters as
pointer types
+ changes inside the function are reflected outside the
function as well
+ unlike call by value where the changes do not reflect
outside the function
Example Using Pointers to Pass Data
/* function definition to swap the values */
void swap(int *x, int *y) {
int temp;
temp = *x; // save value at address x
*x = *y; // put y into x
*y = temp; // put temp into y
}
int main(void) {
// local variables
int a = 100;
int b = 200;
printf("Before swap, value of a: %d\n", a);
printf("Before swap, value of b: %d\n", b);
swap(&a, &b);
printf("After swap, value of a: %d\n", a);
printf("After swap, value of b: %d\n", b);
return 0;
}
Summary of Syntax
- you can communicate two kinds of information about a variable
to a function
function1(x);
- you transmit the value of x and the function must be declared
with the same type as x
int function1(int num);
function2(&x);
- you transmit the address of x and require the function
definition to include a pointer to the correct type
int function2(int *ptr);
const Pointer Parameters
- you can qualify a function parameter using the const keyword
+ indicates that the function will treat the argument
that is passed for this parameter as a constant
+ only useful when the parameter is a pointer
- you apply the const keyword to a parameter that is a pointer
to specify that a function will not change the value to
which the argument points
bool send_message(const char *pmessage) {
// code to send the message
return true;
}
- the type of the parameter, pmessage, is a pointer to a
const char
+ it is the char value that's const, not its address
+ you could specify the pointer itself as const too,
but this makes little sense because the address is
passed by value
- you cannot change the original pointer in
the calling function
- the compiler knows that an argument that is a pointer to
constant data will be safe
- if you pass a pointer to constant data s the argument for a
parameter then the parameter must be used as above
Returning Pointers from a Function
- returning a pointer from a function is a particularly
powerful capability
+ it provides a way for you to return not just a
single value, but a whole set of values
- you would have to declare a function returning a pointer
int *name_of_function() {
.
.
.
}
- be careful though, there are specific hazards related
to returning a pointer
+ use local variables to avoid interfering with
the variable that the argument points to