Taught By

Jean-Cédric Chappelier

Dr.

Jamila Sam

Dr

Transcript

Let's end with two fairly easy-to-understand notions. First, default arguments. In function prototypes, we can give default values to some parameters of the function. And in that case, it becomes unnecessary to supply values, that is, arguments, to these parameters when we call the function. To define such a parameter with a default value, all that is needed is to add, in the function's prototype, an equals sign ('=') after the parameter, followed by the default value that we wish to give to this parameter. Kind of like an assignment. And be careful, these parameters with default values must absolutely appear last in the list of parameters of the function. Let's take as an example a function which will display a line of several characters. The prototype of this function is here, the name of the function is <i>affiche_ligne</i> (TN: means "<i>display_line</i>"). This function takes two parameters: the first parameter will define the character that we wish to repeat on the line. The type, <i>char</i>, simply means that the parameter <i>elt</i> will contain a character. The second parameter of the function is the number of times that we wish to repeat the character. And you can see that this parameter has a default value of 5. Which means that when I call the <i>affiche_ligne</i> function in this way, that is, with a single argument, where the first argument is the '*' character (star), The parameter <i>elt</i> will contain the star character. Since I don't have a second argument in this call to my <i>affiche_ligne</i> function, the second parameter <i>nb</i> will take the default value of 5. And so, this call to the <i>affiche_ligne</i> function will display a line with 5 stars. Now, the second call to my <i>affiche_ligne</i> function has two arguments, where the first argument is the '+' (plus) character and the second argument has a value of 8. In this case, the parameter <i>elt</i> will contain the '+' character and the second parameter <i>nb</i> will not take the default value of 5, but the value which is passed as an argument here, which is 8. And so this call to my <i>affiche_ligne</i> function will display the '+' character eight times. Two remarks on this concept of default arguments: arguments with default values must be specified in the function prototype and not in the definition of the function. What's more, upon calling a function with several parameters with default values, omitted arguments must be the last ones and omitted in the order of the parameter list For example, look at this function which has a first parameter called "i" which has no default value, a second parameter "c" which has the character "a" as default value; and a third parameter "x" with a value of 0 by default. If I now call my function "f" in this way, that is, with one single argument, then the parameter "i" will take on the value of 1, the parameter "c" will take the default character, "a", and the parameter "x" will take its default value which is 0. I can also call my function "f" in this way, with two arguments. "i" will take the value of the first argument, that is, 1, "c" will contain the second argument, that is, the character "b", and the third parameter "x" will still take its default value of 0. Now, if I try to call my "f" function in this way, the compiler will refuse the call because the second argument here is of type <i>double</i>. The compiler will try to assign this <i>double</i>-type value to the second parameter "c" which is of type <i>char</i>, so this call will be refused by the compiler. Now, we might be tempted to call our "f" function like this, that is, by omitting the second parameter. Unfortunately, the compiler will refuse such a call. And finally, if I try to call my "f" function like this, with these three arguments, what will happen? The parameter "i" will take a value of 1, the parameter "c" will contain the character "b", and the parameter "x" will take the value of the third argument, a value of 3. We will finish by talking about overloading of functions. In C++, it is possible to define several functions with the same name if these functions have different parameters. That is, if the number or types of these parameters are different. This will allow the compiler to distinguish between these functions. This is called function overloading and it will come in very handy when we use functions which have similar tasks, but operate on different kinds of data. So, in C++, what determines the difference between functions is not only the names of these functions, but also the types of their parameters, which is technically called the "signature" of the function. Note that the return type is not part of these distinctions. You cannot have two functions with the same name and same parameters and simply with different return types. So I can have two functions "f", where the first function has a parameter of type <i>int</i>, and where the second function has a parameter of type <i>double</i>, and where the return type for these two functions is the same, for example <i>int</i>. However, I cannot have two functions "f" which both have a parameter of type <i>int</i> for example, and which vary only in their return type. For example <i>int</i> for the first and <i>double</i> for the second. To sum up, in C++, one can have several functions with the same name, as long as they have different types of parameters. For example, I can have three functions which are all named <i>affiche</i> (<i>display</i>). The first function has a parameter of type <i>int</i>, the second, a parameter of type <i>double</i>, and the third, two parameters of type <i>int</i>. These are indeed 3 different functions. Now, if I call <i>affiche(1)</i>, since the argument 1 is of type <i>int</i>, it is the first function, which has one parameter of type <i>int</i>, that will be called. And in this case, I will get the message "entier : 1". (TN : means "integer: 1") If I now call <i>affiche(1.0)</i>, since the argument (1.0) is of type <i>double</i>, it is the second function, which has one parameter of type <i>double</i>, that will be called, and it will display the message "reel : 1" (TN : means "real : 1"). If I call <i>d'affiche(1,1)</i>, since both arguments have the same <i>int</i> type, it is the third function that will be called and I will get, on the display, "couple : 11". Careful: I cannot define these 2 functions in the same program. The first function has a parameter of type <i>int</i>; the second has two parameters, both of type <i>int</i>, but the second parameter has a default value. If I now call <i>affiche(2)</i>, it is impossible to know whether I want to call the first function "affiche" by passing 2 as an argument to the parameter "x", or the second function, by passing 2 to the parameter "x1" and using the default value of 1 for parameter "x2". There is an ambiguity between these two functions in this call. This is why defining these two functions together in the same program is forbidden.

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.