|
| 1 | +// TODO: We sometimes encourage you to keep trying things on a given exercise |
| 2 | +// even after you already figured it out. |
1 | 3 | // |
| 4 | +// Evey programmer knows that functions are resuable pieces of code. |
2 | 5 | // |
| 6 | +// Each function is called and assigned/"pushed" on to the stack (fragment of memory). |
| 7 | +// Read more about the stack here: https://en.wikipedia.org/wiki/Stack-based_memory_allocation/ |
| 8 | +// |
| 9 | +// A C++ function includes: |
| 10 | +// - The function signature: int f(int x) |
| 11 | +// | | | |
| 12 | +// | | parameter list (parameter datatype parameter name, ...) |
| 13 | +// | | |
| 14 | +// | function name |
| 15 | +// | |
| 16 | +// function return type |
| 17 | +// |
| 18 | +// - Function clause/body: |
| 19 | +// { -> function scope beginning |
| 20 | +// return x; -> return (depends on function return type; will be explained in an upcoming exercise) |
| 21 | +// } -> function scope ending |
| 22 | +// |
| 23 | +// Functions are normally named using the camelCase convention. |
3 | 24 | // |
4 | 25 | // https://www.learncpp.com/cpp-tutorial/introduction-to-functions/ |
5 | 26 | // https://www.learncpp.com/cpp-tutorial/function-return-values-value-returning-functions/ |
| 27 | +// https://www.learncpp.com/cpp-tutorial/why-functions-are-useful-and-how-to-use-them-effectively/ |
6 | 28 |
|
7 | 29 | #include <gtest/gtest.h> |
8 | 30 | #include <iostream> |
9 | 31 |
|
10 | | -int function01(int x) |
| 32 | +// TODO: Create the function signature. |
| 33 | +// Add the function return type, name "f" and a parameter "x" with an int datatype. |
| 34 | +int f(int x) |
11 | 35 | { |
12 | 36 | int y{x + 1}; |
13 | 37 |
|
| 38 | + std::cout << "x: " << x << " " << "y: " << y << std::endl; |
| 39 | + |
14 | 40 | return y; |
15 | 41 | } |
16 | 42 |
|
| 43 | +int main(int argc, char **argv) |
| 44 | +{ |
| 45 | + // TODO: Call the function and pass in an appropriate parameter. |
| 46 | + f(10); |
| 47 | + |
| 48 | + std::cout << "\n\n" << "Testing output begins here\n--------------------------" << "\n"; |
| 49 | + testing::InitGoogleTest(&argc, argv); |
| 50 | + return RUN_ALL_TESTS(); |
| 51 | +} |
| 52 | + |
17 | 53 | TEST(Functions, Function01) |
18 | 54 | { |
19 | | - ASSERT_EQ(function01(0), 1); |
20 | | - ASSERT_EQ(function01(1), 2); |
21 | | - ASSERT_EQ(function01(2), 3); |
| 55 | + ASSERT_EQ(f(0), 1); |
| 56 | + ASSERT_EQ(f(1), 2); |
| 57 | + ASSERT_EQ(f(2), 3); |
22 | 58 | }; |
0 commit comments