In a previous episode, we learned about the three aspects of methods. We discussed calling methods, as well as what a method header is. In this episode, we will concentrate specifically on the concept of method definitions. If I go back to our usual example of the mean calculation, the definition of the "moyenne" (TN: means "mean") method is this group of lines beginning with the header, and comprising all the lines here that follow the header, between braces. The definition of a method serves, as its name implies, to define what the method must do, that is, to specify the body of the method, i.e the set of instructions which are the reason why we decided to isolate this method, to create it in the first place. From a syntax point of view, we will start by providing the header, -- as a reminder, this is the specification of the return type followed by the name of the method, followed between parentheses by the list of parameters -- but in the definition, this header will then be followed by a bloc which will contain the instructions that will allow the method to work, which describe how the method will run. Among these instructions, we will have one or more "return" instructions which will terminate the execution of the body of the method If I go back to the example of the "moyenne" method, we can see the header of the method, as we have seen it in the previous examples, followed here by the set of instructions which allow the "moyenne" method to actually do what it is meant to do, which is reduced here to a simple "return" statement which will calculate the sum of these two parameters, x and y, and divide it by 2. The body of the method is thus simply a bloc of Java code, that is a set of instructions between curly braces which define what the method must do. And in this bloc, we can use the parameters of the method as variables. The value returned by the method is indicated by a particular expression, which is what we call a "return statement", and which begins by the keyword "return" and is followed by a Java expression that is evaluated and that will define the return value of the method. It must thus of course be of the same type as the return type of the method. If I take the example of the mean calculation, the return type we have here is "double", for the mean, and so we will have, behind the "return" keyword, a Java expression of type "double" which will be the value returned upon calling the "moyenne" method. The return statement actually does two things: it starts by defining the return type of the method and then the first return statement ends the execution of the method in which it is called. The return statement is sometimes reduced to a single variable; for example we could come across expressions like "return x;" or even simply a value, such as "return 3;". These are only specific cases with simple expressions, a single variable or a particular value, but it is by far not the general case and, as we saw in the previous example, in the general case it is a full Java expression which is found after the "return" keyword. A few comments about return statements. First of all, it is easily possible to have several return statements in the body of one method. For example if I have a method here which will return the maximum of two "doubles", 'a' and 'b' received as parameters, I can write this method in the following way. I declare a variable 'm' here that I will use to return the maximum of 'a' and 'b'. If 'a' is greater than 'b', then I copy the value of 'a' into 'm'; otherwise, if 'a' is lesser than or equal to 'b', this "if" statement jumps here and it will of course be the value of 'b' which will be copied into 'm'. But we could absolutely have defined this method in a more compact way, as "max2" here, with simply two return statements. This works like that: if 'a' is greater than 'b', then we will enter here and execute the "return a;" statement and so this statement will end the execution of the body, and the maximum of 'a' and 'b' in this case will be the value of 'a'. However, if 'a' is lesser than or equal to 'b', the "if" statement here will jump to the "else" and so we will execute this return statement here which will return to the rest of the program the value of 'b'. Second comment concerning the return statement: as mentioned earlier, the return type following the "return" keyword must be the same as the type declared in the header. For example here, if I consider a dummy method which takes no parameter and which returns a "double". Then I cannot return an expression which would evaluate to a type different from "double", for example here I cannot return a "boolean" value. It would throw an error during compilation. Third comment concerning return statements: a return statement must be the last instruction that the compiler will encounter. There cannot be any statements beyound the return statement. If we take, for example, this method whose goal is to read a "double" [from the keyboard], this method does not receive any parameters; it will first ask for a number, then read a number from the standard input, the keyboard typically. It will then assign to a "double" declared here as 'n' the value read from the keyboard, represented here as "keyb", and will return this value 'n'. But we find here in the "lire" method [TN: "lire* means "read"] a display of a line break. This is absolutely impossible, we cannot have such an instruction following a return statement. The return statement must be the last statement to be executed, otherwise the compiler will throw an error. Last comment concerning return statements. The compiler must guarantee that we can always execute at least one return statement. So for example if I take another version of the "lire" function that we have just encountered, whose beginning is exactly the same -- we will read a "double" from the keyboard -- but assume that here, we only want to return positive numbers. So we add a condition here, if 'n' is strictly positive. If 'n' is strictly positive then we will return 'n'. Up until now there is no problem. However, at this stage, it is possible for the user to have entered a negative number, for example they could easily have entered -1. This would cause the "if" condition to be false and so we would jump over the "if" bloc, and end up here by executing nothing. The compiler will not always be able to execute the return statement. It will thus throw an error. A possible correction for this code would of course have been to bunch together the request for a number and the return value into a "while" loop for example. While 'n' is lesser than or equal to 0, we would repeat the request for a positive number, thus guaranteeing that we will always end with a "return" when the number entered is strictly positive. More specifically, since we will start by asking the user for the value before being able to test whether this value is positive or null, it will actually be a "do while" loop, which we will be able to write here. Then we can wonder about the condition, which is here to return, to stop when we have a strictly positive number. This means that we will want to loop, to repeat the question as long as the number is negative or null. We will repeat this until we reach the condition we wanted to have to return. OK, so here we will add the "return n;" and while 'n' is not how we want it to be, while 'n' is negative or null, then we will repeat, we will loop. We will first display the message, so, this line, then we will read the answer, so we will add this line here. Concerning the variable declarations, we have to declare the scanner and declare 'n', all these lines here, outside the loop since we don't need to repeat the scanner in the loop and we only need to declare 'n' once. So basically, this gives the following code: we will start by declaring the scanner, once and for all, for the keyboard input; then we will declare the variable 'n'. It must be declared outside of the loop since we will use it here to return it. We also use it in the test, which is outside of the scope of the loop's body. So we declare this variable and initialize it to a value which makes sense, let's say 0. Then, we write the loop bloc here, the bloc which we want to repeat, in which we will display the message which asks the user to enter a number. Here, we can even specify our intentions exactly so that the user knows what to do, we can say that we want a strictly positive number. Then, we read this number into 'n' from the keyboard and so we loop while we haven't received the right answer, while 'n' is negative. This is how we could have corrected the error of the missing "return" when 'n' was equal to or lesser than 0. Let's now present a few specific examples of methods; starting with methods without return types. I must first remind you that a method in the Java sense has nothing to do with a mathematical function. A method in Java is simply a piece of code that we reserved to be able to use it, to avoid repetition, and in this sense, this piece of code does not necessarily need to return anything to the rest of the program. If this is the case, then we will specify the fact that the method returns nothing by using a specific return type, the "void" type, to indicate that we will not return anything to the rest of the program. In such a case, the "return" instruction is now optional. We can absolutely include no return statement at all in the method's body. If we really need one, for example in an "if" statement, in order to stop the execution of the body at a precise moment, then we will be able to use a return statement. For example, let's take a method which I will call "afficheRacine" [TN: means "displaySquareRoot"] and whose job is to display the square root of a double-type parameter received here and named "a". If 'a' is negative, then as you know, we cannot compute the square root and thus there is nothing to display. So in this case, if the condition for 'a' to be negative is true, then the "if" will go to this return statement -- there is only a single "return" here -- and this will thus end the execution of the method. Note that since the return type of the "afficheRacine" method is "void", the "afficheRacine" method has nothing to return to the rest of the program and you can see here that the "return" keyword is not followed by any expression -- it would make no sense to have one, we wouldn't know what to put after this "return". If however 'a' is positive or null, then the "if" condition is of course false and at that moment, the "if" bloc is simply ignored and the program will continue after the "if" to execute the display of the square root of 'a'. And we then arrive to the end of the method, where we did not write any return statement. The method will end here, and it is not at all necessary to add any "return" here; such a return statement is totally optional. Other specific example of methods: methods without parameters. We already discussed these a little but I would like to go over them again. These are methods to which we do not need to supply values from the rest of the program in order for these methods to work. And in that case, we simply need to specify, in the header, the empty parameter list by writing an opening parenthesis followed by a closing parenthesis with nothing in between. Let's take an example of a method whose only goal is to ask the user for an integer and return it to the rest of the program. An example of a call to this method could be as follows: here, we have a variable of integer type which we initialize with the result of the call to "saisieEntier" [TN: means "inputInteger"]. This method doesn't need to receive anything from the rest of the program; it can work completely autonomously by declaring a variable locally in which it will store the user's answer. It will display the request to enter an integer for the user, read the integer input by the user on the keyboard, and put it into the temporary, local variable, named "i" here, and return this variable 'i' to the rest of the program You can clearly see that to do all of this, the method did not need to receive any information from the rest of the program, it is thus a method without parameters. As a handy aside: up until now, we only needed the scanner in the "main" method. We only had one method up until now and we used the scanner in the "main" method. That is why we declared it there, in the "main" method. Here, we need it in another method, the "saisieEntier" method. So of course we could declare a scanner like this in the "saisieEntier" method, but it is not really a good idea because each time you call "saisieEntier" -- and the idea of this function is that it will be called several times -- every time you call it, every time you use the "saisieEntier" method then this line which declares the scanner will be executed every time and you will re-declare a keyboard every time. However, we have only one keyboard, so the idea would be to share this keyboard across the whole program and thus to put this line here outside of the class, outside of the method here. So we find the declaration here, "static scanner clavier etc.", with the link to the keyboard "System.in". We will add, for reasons which will be explained in the object-oriented course, the "private" keyword here, for good practice. At this stage, if you need a scanner in other methods than the "main" method, I encourage you to follow this syntax here. To finish, I would like to talk about the "main" method. Yes, it is also a method! It is simply a method whose name and header are imposed. It is the method with which any Java program will start, it will be the first method to be executed when we start the program. The header imposed for the "main" method is as follows where, without going too much into detail, we can see that "main" takes, as an array of character arrays, a list of arguments that would come from the outside, that would come from the program which calls another program. But this would lead us a bit too far, and we will not use it in this class. All that you need to know is that the prototype of "main" must be the one indicated here.