Skip to content

Commit 113e335

Browse files
committed
Neaten C++ and MATLAB tutorials
1 parent a3d281f commit 113e335

File tree

2 files changed

+59
-153
lines changed

2 files changed

+59
-153
lines changed

C++/02 C++ - Functions and File IO.md

+53-73
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Functional C++ Crash Course
1+
# Function C++ Crash Course
22

33
Author: methylDragon
44
Contains a syntax reference for C++
@@ -15,28 +15,22 @@ I'll be adapting it from the ever amazing Derek Banas: https://www.youtube.com/w
1515
- Linux (**Terminal/Console proficiency**) (We're going to need to compile our stuff)
1616
- Gone through the all preceding parts of the tutorial
1717

18-
19-
2018
## Table Of Contents <a name="top"></a>
2119

2220
1. [Introduction](#1)
23-
2. [Functional C++ Syntax Reference](#2)
21+
2. [Function C++ Syntax Reference](#2)
2422
2.1 [Functions](#2.1)
2523
2.2 [Function Overloading](#2.2)
2624
2.3 [Recursive Functions](#2.3)
2725
2.4 [File I/O](#2.4)
2826
2.5 [Lambda Functions](#2.5)
2927
2.6 [Inline Functions](#2.6)
3028

31-
32-
3329
## 1. Introduction <a name="1"></a>
3430

3531
We've gone through the basics of C++. Now let's throw in some functions and file interactions!
3632

37-
38-
39-
## 2. Functional C++ Syntax Reference <a name="2"></a>
33+
## 2. Function C++ Syntax Reference <a name="2"></a>
4034

4135
### 2.1 Functions <a name="2.1"></a>
4236

@@ -51,10 +45,10 @@ firstNum and secondNum are attributes. We set secondNum's default value if no va
5145

5246
```c++
5347
int addNumbers(int firstNum, int secondNum = 0) {
54-
int combinedValue = firstNum + secondNum;
55-
56-
return combinedValue;
57-
48+
int combinedValue = firstNum + secondNum;
49+
50+
return combinedValue;
51+
5852
}
5953
```
6054
@@ -72,14 +66,12 @@ addNumbers(1, 2); // It'll return 3
7266
```
7367

7468
> **Note:**
75-
>
69+
>
7670
> - When you write the data-type infront of the function name, you're defining a new function **prototype**
7771
> - If you don't, you're calling it.
78-
>
72+
>
7973
> Remember the distinction!
8074
81-
82-
8375
#### **Returning arrays**
8476

8577
Read more + code source: https://www.geeksforgeeks.org/return-local-array-c-function/
@@ -99,10 +91,10 @@ int *fun()
9991

10092
arr[0] = 10;
10193
arr[1] = 20;
102-
94+
10395
return arr; // Return the pointer to the array!
10496
}
105-
97+
10698
int main()
10799
{
108100
int *ptr = fun();
@@ -122,10 +114,10 @@ int *fun()
122114

123115
arr[0] = 10;
124116
arr[1] = 20;
125-
117+
126118
return arr; // Return the pointer to the array!
127119
}
128-
120+
129121
int main()
130122
{
131123
int *ptr = fun();
@@ -141,17 +133,17 @@ struct arrWrap
141133
{
142134
int arr[100];
143135
};
144-
136+
145137
struct arrWrap fun() // Function returns the struct
146138
{
147139
struct arrWrap x;
148-
140+
149141
x.arr[0] = 10;
150142
x.arr[1] = 20;
151-
143+
152144
return x;
153145
}
154-
146+
155147
int main()
156148
{
157149
struct arrWrap x = fun();
@@ -160,8 +152,6 @@ int main()
160152
}
161153
```
162154
163-
164-
165155
### 2.2 Function Overloading <a name="2.2"></a>
166156
167157
[go to top](#top)
@@ -186,8 +176,6 @@ void sameFunction(int a, float b){
186176

187177
Functions can have the same name, but do DIFFERENT THINGS depending on what gets passed to them!
188178

189-
190-
191179
### 2.3 Recursive Functions <a name="2.3"></a>
192180

193181
[go to top](#top)
@@ -196,21 +184,19 @@ These are functions that call THEMSELVES. Trippy.
196184

197185
```c++
198186
int getFactorial(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;
205193
)
206194
```
207195

208196
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.
209197

210198
The last function calls to go on the stack are resolved first! USE THE STACK! Play some Magic!
211199

212-
213-
214200
### 2.4 File I/O <a name="2.4"></a>
215201

216202
[go to top](#top)
@@ -226,13 +212,13 @@ ofstream writer("dragonQuote.txt"); //Open a .txt file called dragonQuote, this
226212

227213
if(! writer) { //Check to see if the filestream is open
228214

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
231217

232218
} else {
233219

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
236222

237223
}
238224

@@ -246,41 +232,39 @@ ofstream writer2("dragonQuote.txt", ios::app); //Create a writer object that app
246232

247233
if(! writer2) { //Check to see if the filestream is open
248234

249-
cout << "Error opening file" << endl;
250-
return -1; // Return -1 if failed
235+
cout << "Error opening file" << endl;
236+
return -1; // Return -1 if failed
251237

252238
} else {
253239

254-
writer2 << "\n -methylDragon" << endl; // Append "\n -methylDragon"
255-
writer2.close(); // Close the file
240+
writer2 << "\n -methylDragon" << endl; // Append "\n -methylDragon"
241+
writer2.close(); // Close the file
256242

257243
}
258244

259245
{
260-
char letter;
261-
262-
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 {
282256

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+
}
283261

262+
}
263+
264+
cout << endl;
265+
reader.close();
266+
}
267+
```
284268
285269
### 2.5 Lambda Functions <a name="2.5"></a>
286270
@@ -419,8 +403,6 @@ auto y = [&r = x, x = x+1]()->int {
419403
}(); // Updates x to 6, and initializes y to 7.
420404
```
421405

422-
423-
424406
### 2.6 Inline Functions <a name="2.6"></a>
425407

426408
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.
@@ -458,15 +440,13 @@ class S
458440
public:
459441
int square(int s); // declare the function
460442
};
461-
443+
462444
inline int S::square(int s) // use inline prefix
463445
{
464-
446+
465447
}
466448
```
467449

468-
469-
470450
```
471451
. .
472452
. |\-^-/| .

0 commit comments

Comments
 (0)