4.10 Exercice: max of two numbers
Well, here's an exercise for you. e want you to write a function that takes two numbers and returns the maximum of the two. Call this function, give a different argument and make sure it works correctly. So I want you to pause the video to do this exercise and when you're done, come back and continue watching the correction. I'm going to start by defining a function, called max, here we need two parameters, we can call them number 1 and number2, or we can use shorter names like a and b. Before, I told you not to use variable names that don't make sense like a and b, but in this particular case, it doesn't really matter, because a and b are pretty self-explanatory, so we're not dealing with complex logic. So in this function, we want to have some logic, we want to compare the value of a with b, so we're going to use an if statement. So if a is greater than b, we want to return a. Otherwise we want to return b. This is the simplest implementation, it's not the best way, so we're going to optimize step by step, but before we go any further, let's make sure this function actually works. So I'm going to declare a variable called number and set it to the maximum of 1 and 2. Now let's display number on the console. Save the changes, so the max is 2, so if the second argument is larger, then that will mean our function is working. What happens if the first argument is bigger? So we'll change 1 to 3, we record, now we see 3, and if both arguments are equal, again we get 3. Now before we go any further, did you notice how I called this function with different arguments, I called it with different test cases. First I assume that the second argument is greater, then I assume that the first argument is greater, and finally I assume that both arguments are equal. So when writing code functions, you need to test your functions with different possible values, Now, let's go back to this max function and clean up this code. The first thing I want to improve here is to remove this else keyword. Why do I want to do that? Well if 8 is greater than b, we're going to return a and so we're going to skip this function. After line 6, nothing will be executed, it will never get to that point, so we don't really need the else keyword. So if a is greater than b, we'll return a, otherwise we'll return b. So this is a cleaner implementation. But we can make it even cleaner. Previously we discovered the conditional operator. So we add a condition in parentheses. Question mark and if that condition is true, we use one value, otherwise we use the other value. We can rewrite these two lines, using our conditional operator so what is the condition here? If a is greater than b. If this condition is true, we want to return a, otherwise we want to return b. All we need here is a return statement and end this with a semicolon. So this line is exactly equivalent to these two lines. Now save the changes, both arguments are equal, we get 3, so so far so good. Let's try another example. We'll give as the first argument 5, we get 5 on the console, now if we expand the second argument, we'll put for example 10, and we get 10 on the console. So our function works correctly. That's it for this little training exercise, let's meet again for a very next demonstration.