From d2b590e5393d3da5956ec1ba35da748277526a20 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gilles?= <sebastien.gilles@inria.fr>
Date: Wed, 28 Sep 2022 17:27:16 +0200
Subject: [PATCH] Minor changes in the notebooks in procedural programming
 part, including renaming of functions to match the same convention (#32).

---
 1-ProceduralProgramming/1-Variables.ipynb     | 18 ++++++++-----
 .../2-Conditions-and-loops.ipynb              | 26 ++++++++++++-------
 1-ProceduralProgramming/3-Types.ipynb         | 16 +++++++-----
 1-ProceduralProgramming/6-Streams.ipynb       |  2 +-
 .../7-StaticAndConstexpr.ipynb                | 20 +++++++-------
 5 files changed, 49 insertions(+), 33 deletions(-)

diff --git a/1-ProceduralProgramming/1-Variables.ipynb b/1-ProceduralProgramming/1-Variables.ipynb
index 9ee6928..663d21d 100644
--- a/1-ProceduralProgramming/1-Variables.ipynb
+++ b/1-ProceduralProgramming/1-Variables.ipynb
@@ -67,10 +67,10 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "Although not mandatory, it is **strongly** recommended to give a\n",
+    "Although not mandatory, it is **strongly** recommended to give an\n",
     "initial value to your variables, as an expression between brackets. \n",
     "\n",
-    "Not providing an initial value may lead to unexpected behaviour. For instance you can't make hypothesis upon the values of `number` and `real` in the cell above: you might end-up with any value... and someone else might get other values on his computer!\n",
+    "Not providing an initial value may lead to unexpected behaviour. For instance you can't make hypothesis upon the values of `number` and `real` in the cell above: you might end-up with any value... and someone else might get other values on his/her computer!\n",
     "\n",
     "\n",
     "If you give braces without values, a predefined and associated value\n",
@@ -97,7 +97,7 @@
     "C++ actually supports many other historical forms\n",
     "of initialization, which you will encounter everywhere, including in this tutorial,\n",
     "with brackets and/or an equal sign. There are some subtle differences\n",
-    "between each other... that you can ignore most of the time."
+    "between each other... that you can ignore most of the time (still, if you're beginning our advice is to take the habit to use braces which are both less ambiguous and a bit more secure than the others syntaxes)."
    ]
   },
   {
@@ -481,7 +481,7 @@
    "source": [
     "## Pointers\n",
     "\n",
-    "A pointer contains the address of another variable. It declares itself by slipping\n",
+    "A pointer contains the address in memory of another variable. It declares itself by slipping\n",
     "a `*` character before the name. It can be initialized or not with the address\n",
     "of another variable. To explicitly extract the address of this other variable,\n",
     "we use the symbol `&`.\n",
@@ -796,12 +796,16 @@
     "    int a { 2 }, b { 3 };\n",
     "    const int* p { &a }; // Address pointed to is modifiable, but not the underlying value.\n",
     "    \n",
-    "    std::cout << \"Value pointed by p (which doesn't allow value modification) is: \" << *p << std::endl;\n",
+    "    std::cout << \"Value pointed by pointer p (which doesn't allow value modification) is: \" << *p << std::endl;\n",
     "    \n",
     "    int* p2 {&a}; // Pointer to the same memory area, but no constness here.\n",
     "    *p2 = 10;\n",
     "    \n",
-    "    std::cout << \"Value pointed by p (and modified through p2) is: \" << *p << std::endl;    \n",
+    "    std::cout << \"Value pointed by pointer p (and modified through p2) is: \" << *p << std::endl;    \n",
+    "    \n",
+    "    a = -3;    \n",
+    "    std::cout << \"Value pointed by pointer p (and modified through variable directly) is: \" << *p << std::endl;    \n",
+    "\n",
     "}"
    ]
   },
@@ -884,7 +888,7 @@
     "#include <iostream>\n",
     "\n",
     "{\n",
-    "    int k[2][3] { { 5, 7, 0 }, { 3, 8, 9 }};\n",
+    "    int k[2][3] { { 5, 7, 0 }, { 3, 8, 9 } };\n",
     "    \n",
     "    std::cout << \"k[0][0] = \" << k[0][0] << \" (expected: 5)\" << std::endl;\n",
     "    std::cout << \"k[1][2] = \" << k[1][2] << \" (expected: 9)\" << std::endl;\n",
diff --git a/1-ProceduralProgramming/2-Conditions-and-loops.ipynb b/1-ProceduralProgramming/2-Conditions-and-loops.ipynb
index 18f61ae..21028ec 100644
--- a/1-ProceduralProgramming/2-Conditions-and-loops.ipynb
+++ b/1-ProceduralProgramming/2-Conditions-and-loops.ipynb
@@ -115,8 +115,7 @@
     "    }\n",
     "    \n",
     "    std::cout << \"a was not modified and is still 2: \" << a << std::endl;    \n",
-    "}\n",
-    "\n"
+    "}"
    ]
   },
   {
@@ -332,7 +331,7 @@
     "* The variable is an integer, an enum (see [next notebook](./3-Types.ipynb#Enumerations)) or might be convertible into one of those.\n",
     "* The relationship considered is an equality.\n",
     "\n",
-    "I present it quickly in [appendix](../7-Appendix/Switch.ipynb) but we do not have yet seen all the elements needed to explain its interest (which remains fairly limited compared to the powerful `switch` in other languages...)\n"
+    "I present it quickly in [appendix](../7-Appendix/Switch.ipynb) but we do not have yet seen all the elements needed to explain its interest (which remains fairly limited compared to the vastly more powerful `switch` in other languages...)\n"
    ]
   },
   {
@@ -428,9 +427,9 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "The evaluation of the logical operators is performed from the left to the right.\n",
+    "Builtin operators `&&` and `||` perform short-circuit evaluation: they do not evaluate the second operand if the result is known after evaluating the first. \n",
     "\n",
-    "Builtin operators `&&` and `||` perform short-circuit evaluation: they do not evaluate the second operand if the result is known after evaluating the first. Please notice this means these operators aren't commutative!"
+    "Please notice this means these operators are **not commutative**!"
    ]
   },
   {
@@ -450,6 +449,15 @@
     "std::cout << \"b was not incremented!: \" << b << std::endl;"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "The story is different if the ordering of the conditions to test is inverted:"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -467,7 +475,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "Please notice it's true only for built-in operators: later in this tutorial we will deal with operators overloading, and such operators always evaluate both operands."
+    "Please notice it's true **only for built-in operators**: later in this tutorial we will deal with operators overloading, and such operators always evaluate both operands."
    ]
   },
   {
@@ -789,7 +797,7 @@
    "source": [
     "but honestly in more complex cases `break` can help keep the code more readable. \n",
     "\n",
-    "`continue` is related: it is useful when in some conditions you want to skip the remainder of the current loop iteration to go directly to the next:"
+    "`continue` is related: it is useful when in some conditions you want to skip the rest of the current loop iteration to go directly to the next:"
    ]
   },
   {
@@ -822,14 +830,14 @@
     "        \n",
     "        for (int j = 2; j < i / 2; ++j)\n",
     "        {\n",
-    "            if (i % j == 0)\n",
+    "            if (i % j == 0) // % returns the remainder of the division\n",
     "            {\n",
     "                is_prime = false;\n",
     "                break; // this break cuts the inner loop 'for (int j = 1; j < i / 2; ++j)'\n",
     "            }\n",
     "        }\n",
     "\n",
-    "        std::cout << (is_prime ? \" and prime.\" : \".\") << std::endl;\n",
+    "        std::cout << (is_prime ? \" and prime.\" : \".\") << std::endl; // the ternary operator\n",
     "    }\n",
     "}"
    ]
diff --git a/1-ProceduralProgramming/3-Types.ipynb b/1-ProceduralProgramming/3-Types.ipynb
index 69d5c07..fab40c4 100644
--- a/1-ProceduralProgramming/3-Types.ipynb
+++ b/1-ProceduralProgramming/3-Types.ipynb
@@ -141,7 +141,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "A shortcoming of historical `enum ` is that the same word can't be used in two different `enum`:"
+    "A (huge) shortcoming of historical `enum ` is that the same word can't be used in two different `enum`:"
    ]
   },
   {
@@ -282,14 +282,14 @@
     "\n",
     "The _0 notation column_ is the way to notice explicitly the type in an expression; of course any value might be used instead of 0. A `u` might be used to signal the unsigned status for integer types; for instance `3ul` means 3 as an _unsigned long_. `auto` notation below will illustrate a case in which such a notation is useful.\n",
     "\n",
-    "The STL features rather heavily a type named `std::size_t`, which by design is able to store the maximum size of a theoretically possible object of any type (including array). On most (all?) systems `std::size_t` is an alias to an `unsigned long`. More may be found about this type on [CppReference](https://en.cppreference.com/w/cpp/types/size_t). \n",
+    "The STL features rather heavily a type named `std::size_t`, which by design is able to store the maximum size of a theoretically possible object of any type (including array). On most (all?) systems `std::size_t` is an alias to an `unsigned long`. More may be found about this type on [CppReference](https://en.cppreference.com/w/cpp/types/size_t). The equivalent counterpart for signed integers is the [`std::ptrdiff_t`](https://en.cppreference.com/w/cpp/types/ptrdiff_t), which is the signed integer type of the result of subtracting two pointers. \n",
     "\n",
     "You might also encounter [`std::ptrdiff_t`](https://en.cppreference.com/w/cpp/types/ptrdiff_t), which is akin to `std::size_t` but for *signed* values. It is typically used in the STL to store the result of subtracting two pointers. \n",
     "\n",
     "\n",
     "#### Numeric limits\n",
     "\n",
-    "Always keep in mind the types of the computer don't match the abstract concept you may use in mathematics... See [link](https://en.cppreference.com/w/cpp/types/numeric_limits). The types stored especially don't go from minus infinity to infinity:"
+    "Always keep in mind the types of the computer don't match the abstract concept you may use in mathematics... The types stored especially don't go from minus infinity to infinity:"
    ]
   },
   {
@@ -330,6 +330,8 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
+    "(see the [CppReference dedicated page](https://en.cppreference.com/w/cpp/types/numeric_limits) for more details about `std::numeric_limits`).\n",
+    "\n",
     "If an initial value is not in the range, the compiler will yell:"
    ]
   },
@@ -367,7 +369,7 @@
     "    unsigned int max = std::numeric_limits<unsigned int>::max();\n",
     "    \n",
     "    std::cout << \"Max = \" << max << std::endl;\n",
-    "    std::cout << \"Max + 1 = \" << max + 1 << \"!\" << std::endl;\n",
+    "    std::cout << \"Max + 1 = \" << max + 1 << \" // !\" << std::endl;\n",
     "}"
    ]
   },
@@ -381,7 +383,7 @@
     "\n",
     "The most obvious way to avoid this is to choose appropriate types: if your integer might be huge a `long` is more appropriate than an `int`.\n",
     "\n",
-    "Other languages such as Python gets a underlying integer model that is resilient to this kind of issue but there is a cost behind; types such as those used in C++ are tailored to favor optimization on your hardware."
+    "Other languages such as Python gets a underlying integer model that is resilient to this kind of issue but there is a cost behind it; types such as those used in C++ are tailored to favor optimization on your hardware."
    ]
   },
   {
@@ -817,7 +819,9 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "metadata": {},
+   "metadata": {
+    "scrolled": true
+   },
    "outputs": [],
    "source": [
     "#include <vector>\n",
diff --git a/1-ProceduralProgramming/6-Streams.ipynb b/1-ProceduralProgramming/6-Streams.ipynb
index e640641..9ae1b5d 100644
--- a/1-ProceduralProgramming/6-Streams.ipynb
+++ b/1-ProceduralProgramming/6-Streams.ipynb
@@ -467,7 +467,7 @@
    "source": [
     "It is however useful to be aware of the pre-C++ 11 syntax, especially for the number to string conversion: 'arithmetic' operations between strings (as `+`) incur copies that are avoided with the `std::ostringstream` syntax... but the construction of such a `std::ostringstream` object is costly as well...\n",
     "\n",
-    "C++ 20 should provide a better looking and more efficient syntax with `std::format` (see [this page](https://en.cppreference.com/w/cpp/utility/format/format) for more details).\n"
+    "C++ 20 should provide a better looking and more efficient syntax with `std::format` (see [this page](https://en.cppreference.com/w/cpp/utility/format/format) for more details)... but unfortunately current support by compilers is [not great](https://en.cppreference.com/w/cpp/compiler_support).\n"
    ]
   },
   {
diff --git a/1-ProceduralProgramming/7-StaticAndConstexpr.ipynb b/1-ProceduralProgramming/7-StaticAndConstexpr.ipynb
index 1476b14..2d7c8ea 100644
--- a/1-ProceduralProgramming/7-StaticAndConstexpr.ipynb
+++ b/1-ProceduralProgramming/7-StaticAndConstexpr.ipynb
@@ -137,14 +137,14 @@
    "source": [
     "// Recursive function - Xeus cling may not appreciate if you call this cell several times.\n",
     "\n",
-    "auto fibonacci (std::size_t n) \n",
+    "auto Fibonacci (std::size_t n) \n",
     "{\n",
     "    if (n == 0)\n",
     "        return 0;\n",
     "    if (n == 1)\n",
     "        return 1;\n",
     "    \n",
-    "    return fibonacci(n-1) + fibonacci(n-2);\n",
+    "    return Fibonacci(n-1) + Fibonacci(n-2);\n",
     "}"
    ]
   },
@@ -155,8 +155,8 @@
    "outputs": [],
    "source": [
     "#include <iostream>\n",
-    "std::cout << fibonacci(5) << std::endl;\n",
-    "std::cout << fibonacci(10) << std::endl;"
+    "std::cout << Fibonacci(5) << std::endl;\n",
+    "std::cout << Fibonacci(10) << std::endl;"
    ]
   },
   {
@@ -165,7 +165,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "double array[fibonacci(5)]; // COMPILATION ERROR!"
+    "double array[Fibonacci(5)]; // COMPILATION ERROR!"
    ]
   },
   {
@@ -188,14 +188,14 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "constexpr auto fibonacci_const (std::size_t n) \n",
+    "constexpr auto FibonacciConstexpr (std::size_t n) \n",
     "{\n",
     "    if (n == 0)\n",
     "        return 0;\n",
     "    if (n == 1)\n",
     "        return 1;\n",
     "    \n",
-    "    return fibonacci_const(n-1) + fibonacci_const(n-2);\n",
+    "    return FibonacciConstexpr(n-1) + FibonacciConstexpr(n-2);\n",
     "}"
    ]
   },
@@ -205,7 +205,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "double array[fibonacci_const(5)]; // Ok!"
+    "double array[FibonacciConstexpr(5)]; // Ok!"
    ]
   },
   {
@@ -225,7 +225,7 @@
     "\n",
     "int i = 7;\n",
     "++i; // i is by no stretch a compile time variable!\n",
-    "std::cout << fibonacci_const(7) << std::endl;"
+    "std::cout << FibonacciConstexpr(i) << std::endl;"
    ]
   },
   {
@@ -234,7 +234,7 @@
    "source": [
     "`constexpr` becomes increasingly powerful over time:\n",
     "\n",
-    "- The function `fibonacci_const` above does not in fact work before C++ 14: this version of the standard introduced the possibility to provide several `return` in a `constexpr` function.\n",
+    "- The function `FibonacciConstexpr` above does not in fact work before C++ 14: this version of the standard introduced the possibility to provide several `return` in a `constexpr` function.\n",
     "- `constexpr` is added wherever possible in the STL. This is still a work in progress: if you look for instance the [CppReference page](https://en.cppreference.com/w/cpp/algorithm/count) for `std::count` algorithm, you will see this algorithm becomes `constexpr` in C++ 20.\n",
     "\n",
     "We will see another use of `constexpr` in a [later notebook](../4-Templates/2-Specialization.ipynb#If-constexpr)."
-- 
GitLab