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
Copy file name to clipboardExpand all lines: 04-functions.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -90,15 +90,15 @@ int main() {
90
90
}
91
91
```
92
92
93
-
In fact, the call of `abs_value()` yielding its return value could be used directly in the second `cout` call, which means a named variable `a` is not needed. Using a (temporary) variable to store the return value of a function could be seen as unnecessary if the value is used only once, however if the return value of a function is needed more than once and is not stored in a variable, the function must be called multiple times which could become inefficient.
93
+
In fact, the call of `abs_value()` yielding its return value could be used directly in the second `cout` call, which means a named variable `a` is not needed. Using a (temporary) variable to store the return value of a function could be seen as unnecessary if the value is used only once, however if the return value of a function is needed more than once and is not stored in a variable, the function must be called every time its return value is needed, which could become inefficient.
94
94
95
95
**Experiment**
96
96
97
97
* Modify `main()` so that the variable `a` is not needed.
98
98
99
99
* Modify `abs_value()` so that the keyword `else` is used. Does this make the code any more obvious in intent? Do you get a warning about there being no `return` keyword outside of the `if`-`else` clauses? What happens if you add a third `return` statement just before the function's closing brace?
100
100
101
-
* Rearrange the order of the definitions (all beginnning with `int`). What errors do you get?
101
+
* Rearrange the order of the variable and/or function definitions (all beginning with `int`). What errors do you get?
102
102
103
103
## Parameters by value
104
104
@@ -333,7 +333,7 @@ f(): int: 1
333
333
f(): double: 2.5
334
334
```
335
335
336
-
The function to be used is determined at compile-time from the usage at the call site, as the types of the arguments are always known. A best-match is performed in the case of no exact match, so for example `f('a')` would call `f(int)` while `f(0.5f)` would call `f(double)`.
336
+
The function to be used is determined at compile-time from the usage at the call site, as the types of the arguments are always known. A best-match is performed in the case of no exact match, so for example `f('a')` would call `f(int i)` while `f(0.5f)` would call `f(double d)`.
337
337
338
338
**Experiment**
339
339
@@ -346,12 +346,12 @@ Variables declared `static` inside a function body are in fact global variables
346
346
```cpp
347
347
// 04-static-var.cpp : preserving function state in a static variable
348
348
349
-
#include <iostream>
349
+
#include <print>
350
350
using namespace std;
351
351
352
352
void f() {
353
353
static int s{1};
354
-
cout << s << '\n';
354
+
println("{}", s);
355
355
++s;
356
356
}
357
357
@@ -425,12 +425,12 @@ There are three main new things to notice about this program.
425
425
426
426
## Inline functions
427
427
428
-
Functions can be declared as inline functions by using the keyword `inline` before the return type in the function definition. The main aim of declaring a function `inline` is to remove the time overhead of a function call; the code is replicated for each function call *in place* at the call site(s). Functions declared with `inline` must be present (and identical) in each translation unit that uses them, hence they often appear in header files; this is a special relaxation of the ODR. Overuse of inline functions can lead to *code-bloat*, so they are best reserved for very short functions. The following program demonstrates use of the `inline` keyword:
428
+
Functions can be declared as inline functions by using the keyword `inline` before the return type in the function definition. The main aim of declaring a function `inline` is to remove the time overhead of a function call; the function body code is replicated for each function call *in place* at the call site(s). Functions declared with `inline` must be present (and identical) in each translation unit that uses them, hence they often appear in header files; this is a special relaxation of the ODR. Overuse of inline functions can lead to *code-bloat*, so they are best reserved for very short functions. The following program demonstrates use of the `inline` keyword:
@@ -504,7 +504,7 @@ Note that it is **not** necessary (or even possible) to use `if constexpr` for t
504
504
505
505
## Non-returning and noexcept functions
506
506
507
-
It is possible to write a function which never returns, for example using an infinite loop. Another example might be a function that causes an abnormal early exit from the running program; the Modern C++ way of doing this is to throw an exception, or even call `std::terminate()` directly (the C Standard Library also provides `abort()`, `exit()` and `quick_exit()` but these do not deallocate all global objects correctly). The way to indicate this property to the compiler is to use the `[[noreturn]]` attribute when declaring the function, as shown in this example program:
507
+
It is possible to write a function which never returns, for example using an infinite loop. Another example might be a function that causes an abnormal early exit from the running program; the Modern C++ way of doing this is to throw an exception, or even to call `std::terminate()` directly (the C Standard Library also provides `abort()`, `exit()` and `quick_exit()` but these do not deallocate all global objects correctly). The way to indicate this property to the compiler is to use the `[[noreturn]]` attribute when declaring the function, as shown in this example program:
508
508
509
509
```cpp
510
510
// 04-noreturn.cpp : program which does not return from main()
@@ -534,15 +534,15 @@ The keyword `noexcept` is used to declare that a function is guaranteed to not t
534
534
```cpp
535
535
// 04-noexcept.cpp : a noexcept function throwing an exception
536
536
537
-
#include <iostream>
537
+
#include <print>
538
538
#include <stdexcept>
539
539
using namespace std;
540
540
541
541
int throw_if_zero(int i) noexcept {
542
542
if (!i) {
543
543
throw runtime_error("found a zero");
544
544
}
545
-
cout << "throw_if_zero(): " << i << '\n';
545
+
println("throw_if_zero(): {}", i);
546
546
}
547
547
548
548
int main() {
@@ -552,14 +552,14 @@ int main() {
552
552
throw_if_zero(0);
553
553
}
554
554
catch(...) {
555
-
cout << "Caught an exception!\n";
555
+
println("Caught an exception!");
556
556
}
557
-
cout << "Leaving main()\n";
557
+
println("Leaving main()\n");
558
558
}
559
559
```
560
560
561
561
**Experiment:**
562
562
563
563
* Remove the `noexcept` keyword. Does the program compile? What is the output when run?
0 commit comments