In this episode, I would like to sum up the general methodology that I advise you to follow to design and write a function. First of all, I ask you to clearly identify what the function must do. It is a very important point. You must start by identifying what the function must do. It is useless to try to write a function if you don't know clearly what it is you want to do. So this is a very conceptual point. There is no line of C++ to write here. But the point is to design, with the help perhaps of a piece of paper and pencil to draw diagrams, to design what the function will do. Here, we are really interested in what the function does. We are interested in the "what" and for the moment, we do not concern ourselves with the "how", that is, how the function will be implemented. And this is the stage where it is important to watch out for what we have seen in a previous episode on prototypes, the so-called "side effects". In other words, the function must do exactly what it was designed to do, no more, no less. If, for example, you have designed a <i>sqrt</i> function whose goal is to calculate the square root of a <i>double</i>, for example <i>x</i> here, and whose call would correspond to something like <i>z=sqrt(x)</i>, then the <i>sqrt</i> function here must only calculate the square root of <i>x</i> and return it to z. It must not pollute the output in the terminal with displayed messages. It must only calculate, without displaying anything at all, the value of the square root of <i>x</i>. Then, once you have done this, you should ask yourselves: what should the function receive as inputs? What are the arguments that the function needs? Say, for example, I go back to our example <i>moyenne</i> function ("moyenne" means "mean"). In the beginning, I clearly decided that this function's job was to calculate the mean of two real numbers. It would take two real numbers as inputs and it would calculate the mean value of these two real numbers. So at this stage, I'm thinking that it will take two arguments of type <i>double</i>. For example, I can call them <i>x</i> and <i>y</i>. Then, you will ask yourselves whether you should pass your arguments by value or by reference. Let me remind you briefly that there exist two ways of passing arguments: passing by value, which creates a copy and thus does not allow the function the modify the arguments received; and passing by reference, which is used when one wishes to be able to modify the arguments in the function. So here, if I take the example of a call to the "moyenne" function, where we would pass two arguments <i>a</i> and <i>b</i>, the question is: will "moyenne" modify <i>a</i> or <i>b</i>? The answer is clearly no. So here, we will pass them by value. If, however, you think that your function has to modify the arguments that it received, then you should use passing by reference, as we have seen in the video about function calls. Then, optionally, you can wonder if it makes sense to give a default value to each parameter. This will be treated in a subsequent video, but it is at this point that you should ask yourselves this question. Fourth step: ask yourselves which type of value the function must return to the rest of the program. For this, you can write the code in your head like this: z = "function call", with the various arguments. Does it make sense to write z = "function call"? So, for, example, does it make sense to write, z = "the mean of <i>a</i> and <i>b</i>" ? In our case of course, it makes sense, and the sense is that <i>z</i>, at that moment, will be the mean of <i>a</i> and <i>b</i>. So if it makes sense, the return value must be of the same type as <i>z</i>, when this expression makes sense. So here, it will be a <i>double</i> and this means that the <i>moyenne</i> function must return a <i>double</i>. Sometimes, such an expression makes absolutely no sense; it makes no sense to write z = "function call". For example, would it make sense to write z = "display" ? We had the example, earlier, of "display square root". Would it make sense to write z = "display square root of 2" ? Careful, I said "display", and not "calculate square root of 2". What would <i>z</i> be in this case? Personally, I don't see what it could refer to. "Display" displays something and that is all! It has nothing to return here. So this expression, in my opinion, makes no sense and in this case, the return type -- we call this a procedure -- the return type would be <i>void</i>. Fifth and final step, at last. Now, and only now, you can start to preoccupy yourselves with the "how": how to write the function body, how the function will do what it's got to do. Until now, we only worried about the "what" and it is only now, right at the end, that we attack the "how". Let's take the example of the <i>moyenne</i> function, which I know thanks to the previous steps, must return a <i>double</i>. It is called <i>moyenne</i>, it takes an argument <i>x</i> by value, since this argument must not be modified, it takes a second argument <i>y</i>. We will know think about how to write this function. So here I suggest that you start, for example, by declaring a variable which we will call <i>resultat</i> (means "result"), which we will initialize with a value that makes sense, let's say, 0, and to immediately write a <i>return</i> statement which uses this variable. I encourage you to proceed like this every time you have such a function which returns a certain type, for example <i>double</i>. You should define, right away, a variable that you will use as return value. This makes writing the function body easier. Then, that is now, we will think about how to calculate this result. So here, we could write <i>result = (x + y) / 2</i>. Suppose that we wanted to write a function which asks the user for a number between two bounds. The first step is to specify clearly what the function must do. For this, we can start, for example, by writing a comment indicating what the function will do. So here, we will indicate that it is a function which asks for a number between two bounds, for example an integer between 1 and 10. So we will name this function, for example, <i>demander nombre</i> (<i>ask number</i>). Suppose, now, that we have understood well enough what the function must do. We can now go on to the second step, which is to specify the parameters of the function. Here, since we want to ask for a number between two bounds, the parameters that the function needs to work are, of course, the two bounds. Suppose that we are working on integers, here. Then the two bounds will be an integer "borne inférieure" (= "lower bound") and an integer "borne supérieure" (= "upper bound"). So we will receive two integers. Then, we must ask ourselves whether the two parameters passed must be modified by the function or not. If they have to be passed by value or by reference. It seem clear, here, that the lower and upper bounds will not be modified by the action of asking for a number, and so we will pass them by value, as indicated here. We can now proceed to step number 4, which is to know what the return type of the function should be. A good way to answer this question is to ask ourselves: how will the function be called? And here, typically, it would be called with an integer <i>i</i> and we would make a call like this: <i>i = ask_number(between, we said, 1 and 10)</i>. When we have assignments such as this one which can take the return value, where it makes sense to retrieve a return value from the function, then, naturally, the return type of the function must be the type of the variable used in such an assignment. So here, it would make sense to write what I wrote. The type of <i>i</i> would be <i>int</i>, which means that the return type that we will specify for the function will be an <i>int</i> type. We can now return to our program, here, and specify the return type as being an integer. I can now go to the last step. Now and only now, I will attack the function body. For this, I start by writing the body and, small piece of advice here, when your function has a return value, I suggest you immediately declare a variable which will be used for the return. Ideally, you should also initialize it directly and specify that you will return this value. And you can now concentrate on how exactly the function body will be written, you can preoccupy yourselves with the "how". How should we ask for a number between the lower bound and the upper bound? I will leave you to do this as an exercise.