In the previous episodes, we went over the three aspects of functions. We've seen what a function call is, and we have also seen, in another episode, what a function prototype is. In this episode, we will focus on the definition. If I come back to the example of the mean calculation, the definition of the "moyenne" function are these lines here, at the bottom of the program. ["moyenne" means "mean"] We will now focus specifically on this. The definition of a function, as its name implies, serves to define a function technically, that is, to specify the body of the function, the set of instructions that make up this function. Specifically, the definition of a function will start with the header of the function, that is, the beginning of the prototype, without the semicolon where, as you already know, we first define the type, then the name of the function and finally, between parentheses, the set of parameters that we wish to pass to the function. This header is followed, for the definition, by the block which is the body of the function. This body is a set of usual C++ instructions which we put into a block here and which contains one or more return statements which will end the execution of the function body. If I take, as an example, the "moyenne" ("mean") function, we can see here the function header with the double return type, meaning the function will return a double; its name and a list of two parameters. Here, we will calculate the mean of two numbers x and y. In the definition, we have the body of the function which is this block, which is, here, simply one single return instruction and nothing else. The body of the function is simply a block in the C++ sense, that is, a set of C++ instructions contained between an opening brace and a closing brace. The only difference is that in this block, we have access to extra variables which are the parameters of the function. We can use these in this block as if they were any normal variable. The value returned by the function is indicated by the <i>return</i> instruction. This instruction is followed by an expression in the C++ sense, which will be evaluated and will give the value which the function will return. This expression must be of the same type as the function's return type. In the mean calculation example, we have here the body of the function which is this block here, which is very simple in this case, as it contains only one <i>return</i> statement, which will evaluate the expression here, where you can see that we have access to the two parameters x and y. The expression will be evaluated, we will calculate <i>x + y</i>, then we will divide by two, and the result will be of type <i>double</i> and will be returned by the function to the rest of the program. The <i>return</i> statement does two things: it allows us to specify the type of the return value which will be given by the function to the rest of the program and it ends the execution of the function body. As soon as the first <i>return</i> is encountered, the function halts and returns the value corresponding to the expression. Very often, you will find relatively simple return statement which are just variable names, such as <i>return x</i> where x is a variable name. Or even values, such as return 3 to return a value of 3. But this is a specific case, and we can perfectly, as we have seen earlier, use a complete C++ expression. And this is the general case: the evaluation of a complete expression. <i>x</i> and <i>3</i>, here, are simply particular expressions. A few remarks about the <i>return</i> statement on which it is important to insist. First, we can place several <i>return</i> statements in one function. Let's look at a simple example of a function which will return the biggest of two values, let's say, <i>a</i> and <i>b</i>. I call my function <i>max2</i> simply because there already exists a function named <i>max</i> in the C++ standard library. Let's look at the body of this function. We start by declaring a variable <i>m</i> and then, if <i>a</i> is bigger than <i>b</i>, then we copy <i>a</i> into <i>m</i> and if <i>b</i> is smaller than, or equal to <i>a</i>, then the value of <i>m</i> will be the value of <i>b</i>. Then, we return the value of <i>m</i>. However, we could have written this code with two <i>return</i> statements, in a somewhat more compact way. The prototype remains the same, but here, if <i>a</i> is bigger than <i>b</i>, we <i>return</i> <i>a</i>, which means that if <i>a</i> is bigger than <i>b</i>, we will evaluate this expression and return it, and we will stop here. The <i>return</i> ensures that we stop here. If, however, <i>b</i> is smaller than or equal to <i>a</i>, then what will happen is that this <i>if</i> instruction will directly jump here, and now, it is this <i>return</i> which will be executed. Please note that there is only one single <i>return</i> executed for a given function call. Second remark, the expression which follows a <i>return</i> instruction in a function must of course be of the same type as the return type indicated in the function header. For example, here, if I have a function <i>f</i> which does not take any arguments but which returns a <i>double</i>, then all the <i>return</i> statements in this function must be of type <i>double</i>. Here, I have an expression which returns <i>b</i>, of type <i>bool</i>. This is incorrect, and it will generate an error. Third remark concerning the <i>return</i> statement: it must of course be the last instruction executed since the <i>return</i> statement ends the execution of the function. It makes no sense to put instructions after the <i>return</i> statement. Let's take, for example, this <i>lire</i> function ["lire" means "read"], which must return a double after reading it from the standard input. It begins by displaying a message on the standard output and then. it declares a variable of type double called <i>n</i> and initializes it with a value of 0. It reads the value of the double on the standard input and finally, it returns to the rest of the program the value of <i>n</i>. After this, the <i>lire</i> function ends. So there is absolutely no reason to put an instruction after this, it would be completely wrong. Fourth and last remark concerning the return statement. The compiler must always be able to execute a return statement. Here is the example of the <i>lire</i> function again, written slightly differently. We see, again, the request for the user to input a number, the declaration and initialization of the variable <i>n</i>, the reading, from the keyboard, of the value of <i>n</i>; but here, we have a simple test to check if <i>n</i> is strictly positive. If so, the function will execute the return statement and will thus end and return the value of <i>n</i>. But what happens if <i>n</i> is negative? It is quite possible for <i>n</i> to be negative, we can absolutely assume that at this point, the user might have entered a negative value. In such a case, this test here will be false and so the function will jump to the line after the <i>if</i> statement and will end up here. And you can see that from here, there is nothing left to execute. The function will thus end <i>without a return</i> value. This is impossible, so the compiler will throw you an error. The compiler verifies that the function will always end with a <i>return</i> statement, which returns a double, here, since it is the function's return type. In this particular case, what we could have done would have been to correct the function by adding, for example, a loop here that would go back to the top of this block as long as, e.g., the value of n is negative or zero. Then we would repeat the block that asks the user for number and assigns it, which would allow the function to end with a legal value every time. More specifically, since we first want to ask the user for a value before testing whether it is positive or negative, we obviously need a <i>do while</i> loop. So, let's write the <i>do while</i> loop here, with, as condition, that we ask the question again as long as we get a bad answer. Originally, we wanted <i>n</i> to be positive to have a good answer. So we will repeat the loop <i>while</i>, as long as, <i>n</i> is negative. While it is negative, we must repeat this loop and as soon as <i>n</i> will have a positive value , we will be able to return the value. So we can already write the <i>return</i> statement. Then, in the loop, we will get the user's input -- first we ask them the question, and then we save their input. This is what the loop will do. We write the message here, and we read the value into the variable <i>n</i>. Now, we still need to declare this variable <i>n</i> since we use it here as a condition for the <i>while</i> statement which is outside of the <i>do while</i> loop and since we also use it here for the return statement; we will declare this variable outside of the block to be able to use it outside of the block. So we will declare it here, and initialize it with a value that makes sense, for example, 0. So, here is the precise form, which corrects the previous mistake. Now, let's go over a few special cases of functions. Let's start, for example, with a function which has no return type. I must remind you that a function in programming has nothing to do with a mathematical function. In programming, a function is simply a set of instructions that we have put to the side, and named. This set of instructions can certainly have no return value. It will return nothing to the rest of the program. Such a function is sometimes called a "void function". [In French, there is a special word for it: "procedure", not used in English] Such a function will need a specific kind of declaration. The declaration of its type, since it returns nothing, will be done with the reserved word <i>void</i> to indicate that it has no return value. If I take the example here, at the bottom of the slide, the function "affiche racine" [means "display square root"] will simply print something to the terminal and has no need for a return type, since it does not need to return anything to the rest of the program. We will, if you will, "define its return type", indicate the fact that it does not return anything, by using the reserved word <i>void</i>. Since such a function does not return anything to the rest of the program, it needs no return instruction, and the return statement is optional in these functions. However, it could be useful to halt the execution of the function prematurely, for example, depending on the result of an <i>if</i> statement. Here, we can certainly put a <i>return</i> statement in the function body -- and this <i>return</i> will be followed by a semicolon. If I go back to the "affiche racine" function here at the bottom of the slide, which returns nothing at all, since it simply displays on the screen the square root of the argument received. If "a" is negative here, then we should simply display nothing, since the square root of a negative number is not defined. So here, we will halt the execution of the function body with a return statement which returns no expression, which is followed by nothing. If "a", for example, is positive, then the <i>if</i> will not be executed and the function body will continue normally by displaying its square root. Another special case of functions: functions with no parameters. We can of course define functions which need no external parameter to work, to do their job. We have already seen this during the lecture on prototypes: all we have to do is give an empty list of parameters, which is simply a pair of empty parentheses, like this: <i>()</i>. For example, if I want a "saisie" function ["saisie" means "input"] whose job is to ask the user for a <i>double</i> in the terminal. This function must return a <i>double</i> but needs no parameter. So we will declare a variable here, for example, a number of points, that the user will input, which we will define with type <i>double</i>. Here, the type we will use is the same as the type of the return value since it is this variable that we will use to return the value to the rest of the program. We declare this variable here, "nb_points" ["number of points"] and initialize it to zero. Then, we ask the user to input a number of points. We will read the number of points as supplied by the user. And as long as the number of points is negative or larger than a certain threshold, we will loop and ask the user for input again. When we finally have a correct value, we return the number of points supplied by the user to the rest of the program. As you can see, the program which will use, which will call "saisie" [means "input"] does not need to pass any arguments to that function. The "saisie" function works completely autonomously. Last special case of functions: the <i>main</i> function. "Main" is a function like almost any other. It is simply the function which is called right at the beginning of the program. This function has a special prototype. Actually, it has two, but the prototype that we will use in this course is this one: it must return an integer, <i>int</i>, and receive no argument. The standard way of defining <i>main</i> is like this: This integer, <i>int</i>, here, which we return to the rest of the program, is what we return to the program which called our program, that is, the environment in which you will execute your program. And this return value will typically be an error code. By convention, the value 0 indicates that the program was executed correctly. It means that there was no error. It might be a little surprising, but it is a common convention. Zero means that there is no error. Generally, we use error codes bigger than zero, one, two etc. to indicate that there is an error. But that is outside the scope of this lecture. I will just mention the other prototype, the only other prototype accepted for "main", which we sometimes encounter. In some development environments. it is the one you will get by default. It is a prototype which takes two arguments. But it would take us a bit too far to explain what these two arguments are now. In short, they are parameters that we could receive from the environment in which we execute our program.