11# Functions
22
3- ``` admonish warning
4- 🚧 Page Under Construction! 🏗️
5- ```
6-
73Functions are fundamental to programming as they allow us to write reusable pieces of
84code. We have already been using a function in the examples we have shown so far, that
9- is the ` main() ` function which is called by our OS to start the program. We have also
10- seen a function in ` constexpr ` example.
5+ is the ` main() ` function which is called by our OS to start the program.
116
127Functions are defined by introducing a type (or ` auto ` ) followed by the functions name,
138a(n optional) comma-seperated list of parameters surrounded in parenthesis followed by
@@ -16,51 +11,46 @@ suffixing parenthesis to it.
1611
1712``` cpp
1813$#include <iostream>
19- // --snip--
20-
21- auto another_one () {
14+ $
15+ auto foo () -> void {
2216 std::cout << "Another one!\n";
17+ return;
2318}
2419
2520auto main () -> int {
2621 std::cout << "Main function!\n";
2722
28- another_one ();
23+ foo ();
24+ $
2925$ return 0;
30- // --snip--
3126}
3227```
3328
3429``` admonish abstract
3530A function must be declared before it can be used as the compiler has to know the
3631function symbol (name + parameter and return types) exists however, it does not have to
37- defined. Note that the return type must be explicitly stated so the return type can be
38- deduced.
32+ defined.
3933
4034~~~cpp
4135$#include <iostream>
42- // --snip--
43-
36+ $
4437// declaration
45- auto another_one () -> void;
38+ auto foo () -> void;
4639
4740auto main() -> int {
4841 std::cout << "Main function!\n";
4942
50- another_one();
43+ foo();
44+ $
5145$ return 0;
52- // --snip--
5346}
5447
5548// definition
56- auto another_one () -> void {
49+ auto foo () -> void {
5750 std::cout << "Another one!\n";
51+ return;
5852}
5953~~~
60- This mechanism is a result of how C and thus C++ code was and still is compiled and
61- linked together. It allows you to state a symbol existed in a public header (declare)
62- but define it later in a source file which was usually built into a binary library with
63- the linker then connected the calls to the function to the location in the library.
6454```
6555
6656## Parameters
@@ -71,27 +61,22 @@ be specified, using the same syntax we saw to declare a variable
7161
7262``` cpp
7363$#include <iostream>
74- // --snip--
64+ $
7565
76- auto another_one (int const x, int const y) {
66+ auto foo ( const int x, const int y) -> void {
7767 std::cout << "x: " << x << ", y: " << y << "\n";
68+ return;
7869}
7970
8071auto main() -> int {
8172 std::cout << "Main function!\n";
8273
83- another_one(7, 6);
74+ foo(7, 6);
75+ $
8476$ return 0;
85- // --snip--
8677}
8778```
8879
89- ```admonish tip
90- As we saw in the `constexpr` example from the previous page, function parameters may also
91- be declared with `auto` but this can sometimes make hard to know what the type of the
92- parameter is supposed to be.
93- ```
94-
9580## Return Values
9681
9782Functions can also return values using the `return` keyword. The type of the return value
@@ -103,9 +88,8 @@ like we've been using for `main()`. When a function doesn't a value, it's return
10388$#include <iostream>
10489$#include <sstream>
10590$#include <string>
106- // --snip--
107-
108- auto another_one (int const x, int const y) -> std::string {
91+ $
92+ auto foo(int const x, int const y) -> std::string {
10993 auto ss = std::stringstring{};
11094 ss << "x: " << x << ", y: " << y << "\n";
11195 return ss.str();
@@ -115,11 +99,17 @@ auto main() -> int {
11599 std::cout << "Main function!\n";
116100
117101 std::cout << another_one(7, 6);
102+ $
118103$ return 0;
119- // --snip--
120104}
121105```
122106
107+ ``` admonish tip
108+ Previously,
109+ The empty `return` statement can be omitted if the function
110+ ```
111+
112+
123113## Overloading
124114
125115In C++ you can overload functions of the same name to have different implementations
0 commit comments