Hello and welcome to this new lesson of our course, introduction to Java programming. The objective of this new lesson is to teach you to organize your code by modularizing it using your own functions. Functions are an aspect of processing, just like expressions and operators and control structures, which we studied previously. Functions operate, in general, on data, and the data will influence how these functions work. Up until now, the programs that you have written were made up of a linear sequence of instructions with no global organization and with no sharing of repeated tasks. For example, if the task consisting in asking the user for a number, as the following code does, had to be executed several times in our program, for example, once to ask for a number of points, once for a time, and once 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. And in Java, this is done by using what we call functions. Let's consider for example the following program: This program starts by asking for the number of points obtained by a first player, using, as you can see, a <i>do..while</i> loop to force the user to input a value between 0 and 100. Then, the program asks the user for the time taken by the first player which will be, in this case, a value between 1 and 60. And from this number of points and this time, 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 first and second players, to display which of these players has won. You can 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 in the program, once for each player. 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, that is, the code which asks for a number of points, a time and which calculates a score. I will put this code at the end of the program, but before the last closing brace. I will put this code between an opening brace and a closing brace. This code will constitute what we call the body of my function-to-be. We will have to name this body. I will call it <i>saisieEtCalcul</i> (<i>inputAndCalculate</i>). This will allow us to refer to it in the rest of the program, and it will be the name of my function-to-be. My function must give the score that it will calculate to the rest of the program. For this, I will start by removing any references to the first player, since my function will be valid for any player. I will add the <i>return</i> keyword to the end of my function, followed by the name of the variable <i>scoreJoueur</i> (TN: <i>playerScore</i>) to indicate that my function must return the value contained in the <i>scoreJoueur</i> variable. Don't worry, we will go over this in detail in the rest of the lecture. The <i>scoreJoueur</i> variable was defined with a <i>double</i> type, and so the function will return a value of type double. We must indicate this to the rest of the program, and this is done by adding <i>double</i> before the name of the function. Another aspect of functions is that they can use values given by the rest of the program to work. Our function, <i>saisieEtCalcul</i>, 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. Now, all I need to do is add the <i>static</i> keyword to the beginning of this line, for reasons we will see later on in this course. That's it! We have created our first function. I will now use my <i>saisieEtCalcul</i> function to ask for a number of points, a time, and to calculate the score of the first player. 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 <i>scoreJoueur1</i> (<i>player1Score</i>) which I will initialize with the value given by the <i>saisieEtCalcul</i> function. To do this, I will indicate the name of the function followed by opening and closing parentheses. This instruction initializes the <i>scoreJoueur1</i> variable with the value given by the <i>saisieEtCalcul</i> function. This use of the <i>saisieEtCalcul</i> function is known as a <i>function call</i>. I can do exactly the same thing to initialize the second player's score. I can replace all of this code by a simple call to my <i>saisieEtCalcul</i> function. And you can see now that the program is a lot more concise. Creating the <i>saisieEtCalcul</i> function allowed me to avoid duplicating code. It is also interesting, however, to create a function to be able to focus on a somewhat difficult part of the program, such as, here, the calculation of a player's score. As before, I will extract the code I am interested in, put it in between braces, and my new function will return the player's score, a value of type <i>double</i>. I will name my new function, simply, <i>score</i>. For now I will put opening and closing parentheses after the name of the function. As before, my function will return the player's score, thanks to the <i>return</i> keyword. But the difference, compared to what we had before, is that this function now needs the variables <i>nbPoints</i> and <i>temps</i> (TN : <i>time</i>). So in this case, I will add these variables between these parentheses here, which will constitute what we call the parameters of the function. I will return, again, to the place where I extracted the code to create my function, and call my function there instead. I can do that here, for example, I will call my function by supplying it the values of the variable <i>nbPoints</i> and of the variable <i>temps</i>. This is how it's done. 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, like the <i>saisieEtCalcul</i> function, or important as-is, like the <i>score</i> function. It is characterized by a body, which is the program fragment which is reused or emphasized. It is characterized also by its name which will allow us to refer to that function. It can have parameters, which are the set of variables external to the function and which the body needs to work. The function can also return a value, and this value must have a type. Finally, the use of a function in the rest of the program is known as a function call. In object-oriented languages such as Java, the term <i>method</i> is usually used in place of <i>function</i>. From now on, we will say <i>method</i> rather than <i>function</i>. A method thus has three aspects. First, its header, which we can see as a summary of what the method does, since it is made of its name, of its parameters, which correspond to the values which the method needs to work, and of the type of value which the method will return. The second aspect is the definition of the method, which starts with the header, but which is also made up of the body of the method, which contains the code which will be executed when we use the method. The last aspect is the use of the method, or its call, where we will use the method by giving it actual values for its parameters, and where the method will usually return a value that we will be able to use, for example, to initialize a variable. In practice, the developer, that is, the person who will write the definition of the method, is not necessarily the same person as the programmer who will use the method. The programmer using the method does not need to know its body. All that he needs to know is its header, to be able to call it. The header thus acts as a link between the programmer using a functionality and the developer of this functionality. In Java, we speak of APIs, for <i>application progamming interfaces</i> which describe the interfaces for using a set of data. The programmer must respect the header of the method when he calls the method, and the developer will ensure that the header corresponds to the problem that the user wishes to solve by calling the method.