In a previous episode, we introduced the concept of functions and their usefulness in programming. Today, we will look into what happens when we call a function. At this point, we know that the concept of a "function" is associated with 3 aspects: the prototype, the definition and the call. We now will detail what goes on at the moment we call a function. Let's go back to the mean calculation example. Here, we have a little program which asks the user to input two grades. It reads these two grades from the standard input and displays the mean of these two grades. The calculation of the mean is done by calling a function, as you can see here, and it is the specific mechanisms of this function call that we will now study. Let's take a concrete example where we wish to calculate the mean of two numbers, passed to the "moyenne" function as C++ expressions. ["moyenne" means "mean"] The arguments passed to the "moyenne" function upon calling it correspond to the parameters which the function expects in order to run. Note that we usually call the data needed by the function to run correctly as described in its prototype as "parameters", whereas "arguments" refer to the values that we actually pass to the function at the moment we invoke it, at the moment we call it. So we will now dissect the different steps that follow such a function call. First step: we start by evaluating the arguments passed to the "moyenne" function at the moment of the call. In C++, we cannot know in advance in which order this evaluation will take place. In this example, let's decide that it is the second argument which is evaluated first. So we evaluate the second argument, which works out to give a value of 4.25. Then, the first argument is evaluated, which gives a value of 2.3. Second step: the arguments supplied at the function call are matched with the parameters expected by the "moyenne" function. This match is mediated by an assignment -- we will assign to x the result of the evaluation of the first argument, that is, 2.3. And we will affect to y the result of the evaluation of the second argument, that is, 4.25. The "moyenne" function now has actual values in x and y, with which it will be able to work, to run. Third step: we start the execution of the "moyenne" function. Here, all the instructions preceding this final statement, this <i>return</i> statement, will be executed/run. In this case, there is no instruction preceding the <i>return</i> statement, so the third step doesn't do anything in particular. Finally, the <i>return</i> statement is executed. We start by evaluating the expression following the <i>return</i> keyword, so here, this expression is evaluated with actual values for x and y, which produces a value of 3.275. Last step: we make this value, produced by the expression, available to the one who invoked the "moyenne" function by means of the <i>return</i> statement, which means that the function call can now be replaced by the value produced by the function call, that is, 3.275. So, after this last step, the variable z now has a value of 3.275. Here is a small summary of the various steps we have just examined. When a function call happens in a program, five steps follow in the most general case: first step: the expressions which are passed as arguments to the function are evaluated. Second step: the results of these evaluations of expressions are affected to the parameters of the function. Third step: the parameters of the function now have actual values which which the function can work -- the body of the function is executed. Fourth step: the expression which follows the first <i>return</i> statement encountered during execution is evaluated and the result of this evaluation is returned as result of the function call, which basically means that this value replaces the function call. This is what happens in the most general case. The case where a function needs, to work, data as inputs in the form of arguments, of incoming parameters, and returns an actual value. That is the most general case. There are situations where this five-step scheme is somewhat simplified. First situation: there exist cases where a function can, for example, return a result, but does not need any input to work. So, in this case, since there is no use for input arguments, steps 1 and 2 do not happen. Second situation: there are cases where a function can do work, but without returning any actual value. For example: a function can display a certain number of outputs on the terminal, but without returning any values [to the core program]. In this case, steps 4 and 5 do not happen. There is a specific argument-passing scheme, called "passing by reference", which we will examine later on, where step 2 does not happen. We will go over this in detail a little farther in this lesson. Here is another example which illustrates that a function can call another function when running: we have a function "affiche score" [means "display score"] the job of which is to display the score of a player. It needs 3 inputs, it works with 3 parameters: the player, represented here by an integer; the number of points that he won during the game; and the time he spent to gain these points. And the "affiche score" function will simply display the score of the player, which is calculated by using a call to the "score" function. The "score" function's job is to calculate the score and return it as an integer, calculated from the number of points and the game time. So, this shows that a function can absolutely call another function. The only thing to respect, the golden rule, is to prototype the function before calling it. Here, before calling the "score" function, we must have prototyped it. Finally, here is a small summary of function calling. So, a function that was prototyped beforehand can be called in a program. Here, it happens to be called in the body of another function. At the moment of the function call, there is a match being made between the actual arguments and the parameters of the function. Here, what happens is that the argument y is copied into the parameter x. The parameter x now has an actual value with which the body of the function can be executed. The expression following the <i>return</i> keyword is evaluated and made available to the program which called the function, by means of the <i>return</i> statement. So here, the function call is replaced by the result of the evaluation of the <i>return</i> statement an can be assigned to variable of the program which called it. You will have no doubt noticed, during this episode, that there exists a jargon, a terminology associated to function calls. I spoke of "calling" a function, of "passing" arguments to a function. Let's summarize this jargon now. You surely understood that when I speak of calling a function, I refer to the actual fact of using it. Here, I am using the function f to calculate a value which I then store in a variable x. When I say that a value is passed as argument to a function, I mean that this value is copied into a parameter of the function. And finally, when I say that a function returns a value y, I mean that the expression of the function call is effectively replaced by the returned value. Let's take a concrete example here: let's imagine that, if we are dealing with a small function f which returns a result y, let's imagine that, at the moment we call this function, the value of y is 5. Saying that the function returns 5 here simply means that I can replace this call by the return value, that is, 5. So I can just as well say that cosine of 0 returns the cosine of 0, or cosine of 0 returns 1.