Hello! Welcome to this new lesson of the course, Introduction to programming in C++. The objective of this new lesson is to teach you to organize your code by modularizing it using what we call functions. Functions are a part of C++ programs, just like expressions and operators, and control structures, which we have studied previously. Generally, functions operate on data, and these data will influence the functions. Up until now, the programs that you have written were made up of a linear sequence of instructions with no global organization and no sharing of repeated tasks. For example, if the task consisting of asking the user for a number, as the following code does, were to be executed several times in our program. For example to ask once for a number of points, for a time, and for an age. What would you do? You might be tempted to copy the code as many times as necessary to the appropriate places, but, of course, this is a very bad solution. One must never duplicate code when programming. Do not be tempted by the copy-paste. What you would like to copy should be put into a function. Why must one never duplicate code? It makes the program unnecessarily long, hard to understand, and especially, hard to maintain. One would have to apply every modification to each of the copies. This is why every good programming language provides means to allow reusing parts of a program. In C++, this is done using functions. Let's consider for example the following program: This program begins by asking for a number of points obtained by a player. As you can see, it does this by using a do / while loop to force the user to input a value between 0 and 100. Next, the program continues by asking the user to enter the time spent by the player. This time, it is a value between 1 and 60. Then, from the number of points obtained by the player and the time he took, the program calculates the score of the first player. Then, the program repeats these operations for a second player. It asks for a number of points, a time, and calculates the score of the second player. Finally, the program compares the scores of the two players to announce who, of player 1 or player 2, has won. You can thus see that in this program, the code which consists in asking for a number of points, a time, and calculating a score from the number of points and the time is repeated twice, once for each of the two players. In such a case, one must use a function to avoid code duplication. For this, I will first put to the side the code that I am interested in. The code I am interested in is this, the code which asks for a number of points, a time and which calculates a score. So I will put this code to the side. For example here. This code will constitute what we call the body of my function-to-be. We will have to name this body, to be able to refer to it in the rest of the program. I will call it, here, saisie_et_calcul (TN: saisie_et_calcul means input_and_calculation) and this will become the name of my function-to-be. I now want my function to be able to pass on a result to the rest of the program. The result, in this example, is the player's score, which was calculated by the function. I will start by removing the references to player 1, since my function is now valid for any player. I will use the <i>return</i> keyword followed by the variable name score_joueur (TN: score_joueur means score_player). to say that my function must return the value contained in the variable <i>score_joueur</i>. Dont worry, we will go over this in detail in the rest of the course. My function can thus return a value to the rest of the program. This value has a certain type. I must tell the rest of the program what this type is. The value returned by my function is the value contained in the variable <i>score_joueur</i>. <i>score_joueur</i> was defined with a <i>double</i> type. So I have to indicate that my function will return a value of type <i>double</i>. And this is done by adding, before the name of the function, the type of the value returned, which is, here, <i>double</i>. Another aspect of functions is that they can use values given by the outside to work. Our function, saisie_et_calcul, is relatively simple, it doesn't need any such values. To indicate this, we add, after the name of the function, opening and closing parentheses. Our function is complete. I will now use my saisie_et_calcul function to ask the user for a number of points, a time, and to calculate the score of player 1. For this, I will return to the place where I had extracted the code that I used to create my function. That is, here. I will declare a variable which will contain the score of player 1. I will use the function which I named saisie_et_calcul and this will be written like so. The instruction that I have just created, here, is thus a declaration of the variable score_joueur1 (score_player1) and I initialize this variable by using the function saisie_et_calcul. This is known as <i>function call</i>. The call will return the result of the execution of the function. Which is, here, the player's score. This value can then, for example, be stored in a variable, as is the case here. I can do exactly the same thing for the second player. I can remove the code that corresponds to my function that is, this code, and replace it by a call to my function saisie_et_calcul to initialize the variable <i>score_joueur2</i>. You can see that the code is now much more concise. For the moment, my function saisie_et_calcul is defined at the beginning of the program. That is, here. But I could define it elsewhere in the program, for example at the end. Like so. But if I want to use my function to initialize the variables score_joueur1 and score_joueur2, (TN: score_player1 and score_player2) the compiler must know this function at the moment of the function call. To be able to declare this function before its call, I will simply repeat the first line of the definition of my function. I will put this first line before the function call, somewhere here, and end this line with a semicolon. This way, now, when I call my function, the compiler already knows the name of the function and the return type of the function, i.e <i>double</i>, here. This line that declares a function is called a function prototype. Creating the saisie_et_calcul function allowed me to avoid duplicating code. It is also interesting to use a function to focus on a somewhat difficult part of the program, such as, here, the calculation of a player's score. So I will extract, as before, the code which calculates a player's score. And from there, I will create a function in the same way as in my saisie_et_calcul function. This new function will return the player's score, so a value of type double. I will call this new function, simply, <i>score</i>, and for now I will put opening and closing parentheses after the name of the function. I will put the code that I have just extracted between braces. As before, I will use the <i>return</i> keyword to indicate that my function must return the player's score. It will be written like so. The difference with the first saisie_et_calcul function is that this new <i>score</i> function needs the variables <i>nb_points</i> and <i>temps</i> (TN : temps means time) to calculate the player's score. For this I will add these two variables between parentheses. This is written like so. What I have just added between parentheses, here, is called parameters. These are the parameters of my function. Like before, I will go back to where I extracted the code to create my <i>score</i> function to call this <i>score</i> function. I can do it, for example, just after the <i>return</i> keyword. I will call my new <i>score</i> function. This new function requires the number of points, which is this variable here, and the game time, which I will but between parentheses here. To sum up, I first identified the code that was to be put into a function. I extracted this code to create my new function which I named. I identified what we needed as inputs to the function for the function to work. I also identified what my function had to return to the rest of the program. Now I can replace the part of code that I have extracted by a call to my function. A function is thus a reusable fragment of a program, such as the saisie_et_calcul function. or important as-is, like the <i>score</i> function. It is characterized by a body, which the program fragment which is reused or emphasized. It is characterized also by its name which will allow one to refer to that function. It can have parameters, which are the set of variables external to the function and which the body needs in order to work. The function can also return a value, and this value must have a type. And finally, the use of a function in the rest of the program is known as a function call. A function thus has three aspects. First, its prototype, which we can see as a summary of what the function does. Since it contains the function's name, its parameters, corresponding to the values needed by the function to work; As well as the type of value that the function must return. A second aspect is the effective creation of the function, or its definition. Which starts like a prototype but which also contains what we call the function body, which is the code which will be executed when we use the function. The final aspect is thus the use of the function, or its call. We will use the function by giving it actual values for these parameters. The function will return a value that we can easily assign to a variable. In practice, the developer, that is, the person who will write the definition of the function, is not necessarily the same person as the programmer, who will use the function. The programmer using the function does not need to know the body of the function. All that he needs to know is the prototype of the function to be able to call it. The prototype thus acts as a bridge between the programmers using functionnalities and the developers of these functionnalities. The (user) programmer must respect the function's prototype when he calls the function. The developer must make sure that the prototype corresponds to the problem that other programmers want to solve by calling the function. Let's take a new example of a program, which uses a function called moyenne (TN: mean ) to calculate the mean of two values. I think that you are now able to identify the three aspects of this mean function in this new program. First, we have the prototype of the function. As you can see, the function has two parameters which correspond to the two values that we want to average. These two values are of type <i>double</i>. So their mean will also be of type double. The value returned by the <i>moyenne</i> (<i>mean</i>) function will also be of type <i>double</i>. We then have a call to our <i>moyenne</i> function. We can call this <i>moyenne</i> function in this part of the program because the prototype was placed earlier in the program. We give the values of two variables to this function, <i>note1</i> and <i>note2</i> (TN: <i>grade1, grade2</i>) which were first given by the user. This instruction displays the values returned by the <i>moyenne</i> function. Finally, we have the definition of the function here, with its body, that is, the code which defines how the function will calculate the mean of the two values. We will now go over the three aspects, the three aspects of functions.