You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> - When you write the data-type infront of the function name, you're defining a new function **prototype**
77
71
> - If you don't, you're calling it.
78
-
>
72
+
>
79
73
> Remember the distinction!
80
74
81
-
82
-
83
75
#### **Returning arrays**
84
76
85
77
Read more + code source: https://www.geeksforgeeks.org/return-local-array-c-function/
@@ -99,10 +91,10 @@ int *fun()
99
91
100
92
arr[0] = 10;
101
93
arr[1] = 20;
102
-
94
+
103
95
return arr; // Return the pointer to the array!
104
96
}
105
-
97
+
106
98
intmain()
107
99
{
108
100
int *ptr = fun();
@@ -122,10 +114,10 @@ int *fun()
122
114
123
115
arr[0] = 10;
124
116
arr[1] = 20;
125
-
117
+
126
118
return arr; // Return the pointer to the array!
127
119
}
128
-
120
+
129
121
intmain()
130
122
{
131
123
int *ptr = fun();
@@ -141,17 +133,17 @@ struct arrWrap
141
133
{
142
134
int arr[100];
143
135
};
144
-
136
+
145
137
structarrWrap fun() // Function returns the struct
146
138
{
147
139
struct arrWrap x;
148
-
140
+
149
141
x.arr[0] = 10;
150
142
x.arr[1] = 20;
151
-
143
+
152
144
return x;
153
145
}
154
-
146
+
155
147
int main()
156
148
{
157
149
struct arrWrap x = fun();
@@ -160,8 +152,6 @@ int main()
160
152
}
161
153
```
162
154
163
-
164
-
165
155
### 2.2 Function Overloading <a name="2.2"></a>
166
156
167
157
[go to top](#top)
@@ -186,8 +176,6 @@ void sameFunction(int a, float b){
186
176
187
177
Functions can have the same name, but do DIFFERENT THINGS depending on what gets passed to them!
188
178
189
-
190
-
191
179
### 2.3 Recursive Functions <aname="2.3"></a>
192
180
193
181
[go to top](#top)
@@ -196,21 +184,19 @@ These are functions that call THEMSELVES. Trippy.
196
184
197
185
```c++
198
186
intgetFactorial(int number){
199
-
int sum;
200
-
if(number == 1)
201
-
sum = 1; //Yeah you can do this
202
-
else
203
-
sum = getFactorial(number - 1) * number;
204
-
return sum;
187
+
int sum;
188
+
if(number == 1)
189
+
sum = 1; //Yeah you can do this
190
+
else
191
+
sum = getFactorial(number - 1) * number;
192
+
return sum;
205
193
)
206
194
```
207
195
208
196
This function returns the factorial of itself, and it keeps calling itself which results in it calling itself until it stops, then it resolves one by one till all are resolved.
209
197
210
198
The last function calls to go on the stack are resolved first! USE THE STACK! Play some Magic!
211
199
212
-
213
-
214
200
### 2.4 File I/O <a name="2.4"></a>
215
201
216
202
[go to top](#top)
@@ -226,13 +212,13 @@ ofstream writer("dragonQuote.txt"); //Open a .txt file called dragonQuote, this
226
212
227
213
if(! writer) { //Check to see if the filestream is open
228
214
229
-
cout << "Error opening file" << endl;
230
-
return -1; // Return -1 if failed
215
+
cout << "Error opening file" << endl;
216
+
return -1; // Return -1 if failed
231
217
232
218
} else {
233
219
234
-
writer << dragonQuote << endl; // Write dragonQuote to writer, which causes dragonQuote.txt to contain only dragonQuote
235
-
writer.close(); // Close the file
220
+
writer << dragonQuote << endl; // Write dragonQuote to writer, which causes dragonQuote.txt to contain only dragonQuote
221
+
writer.close(); // Close the file
236
222
237
223
}
238
224
@@ -246,41 +232,39 @@ ofstream writer2("dragonQuote.txt", ios::app); //Create a writer object that app
246
232
247
233
if(! writer2) { //Check to see if the filestream is open
ifstream reader("dragonQuote.txt"); // Open an input filestream that reads dragonQuote.txt
263
-
264
-
if(! reader){
265
-
266
-
cout << "Error opening file" << endl;
267
-
return -1;
268
-
269
-
} else {
270
-
271
-
for(int i = 0; ! reader.eof(); i++) { // Read until end of file
272
-
reader.get(letter); // Get the next letter
273
-
cout << letter; // And print it
274
-
}
275
-
276
-
}
277
-
278
-
cout << endl;
279
-
reader.close();
280
-
}
281
-
```
246
+
char letter;
247
+
248
+
ifstream reader("dragonQuote.txt"); // Open an input filestream that reads dragonQuote.txt
249
+
250
+
if(! reader){
251
+
252
+
cout << "Error opening file" << endl;
253
+
return -1;
254
+
255
+
} else {
282
256
257
+
for(int i = 0; ! reader.eof(); i++) { // Read until end of file
258
+
reader.get(letter); // Get the next letter
259
+
cout << letter; // And print it
260
+
}
283
261
262
+
}
263
+
264
+
cout << endl;
265
+
reader.close();
266
+
}
267
+
```
284
268
285
269
### 2.5 Lambda Functions <a name="2.5"></a>
286
270
@@ -419,8 +403,6 @@ auto y = [&r = x, x = x+1]()->int {
419
403
}(); // Updates x to 6, and initializes y to 7.
420
404
```
421
405
422
-
423
-
424
406
### 2.6 Inline Functions <aname="2.6"></a>
425
407
426
408
Inline functions help to avoid the overhead of function calls. When you compile the code, the entire call to an inline function is instead replaced with the code inside the function call itself.
0 commit comments