Section 5 - Default Parameters and Signatures
1A.5.1 Default Parameters
Default parameters are used to allow the client to call a method sometimes with more, sometimes with fewer arguments. Consider this example:
#include <iostream> using namespace std; // ------- function prototype ------- double quadratic(double x, double a, double b=0, double c=0); // ------- main -------------- int main() { cout << quadratic( 3.5, -1, 22.3, 17) << endl; cout << quadratic (3.5, -1, 22.3) << endl; cout << quadratic (3.5, -1) << endl; return 0; } // ------- quadratic -------------- double quadratic(double x, double a, double b, double c) { double ans = a*x*x + b*x + c; return ans; }
Look at the = signs in the function prototype:
double quadratic(double x, double a, double b=0, double c=0);
These are called a default parameters. The caller may choose to omit passing an argument to a default parameter, and if he does not pass one, the value in the initialization is used as if it were passed. All default parameters must be at the end of the formal parameter list, and once the client omits an argument, all remaining arguments must be omitted. Because of these restrictions, usually you would either include all or none of the default parameters. More complex combinations are better handled by function overloading (which we will get to soon).
Notice the three different calls to the function quadratic:
cout << quadratic(3.5, -1, 22.3, 17) << endl; cout << quadratic(3.5, -1, 22.3) << endl; cout << quadratic(3.5, -1) << endl;
- The first passes all four arguments.
- The second passes only three. The 4th parameter gets the default value of 0 which is established in the prototype.
- The third call passes only 2 parameters: the last two parameters are each given the default value of 0. (which, by the way, could have been chosen to be something other than zero, and the two default values could even have been different from one another).

1A.5.2 Method Signatures
When we talk about a function (method) informally, we often omit the parameter details. I might say, "quadratic() this ..." or "quadratic() that ... " in a sentence, but the parentheses are placed there to emphasize that it is a function. They do not mean that the function takes no parameters. The signature of the quadratic() function is shown when we describe it fully. It is double quadratic(double x, double a, double b, double c). This is the official description of the function. It is called the signature of the function or method.
It is very important that you implement functions according to the signature that is specified. Even though I might put empty parens after the method name when discussing it in a sentence, like pow() or quadratic() or SetAge(), you must look for the official description and implement that. If the function signature that you provide does not match 100% with the function signature that is in the program spec, it is as if you did not write that function at all. That's right -- an incorrect signature is worth nothing. The reason is that the rest of your project group assumed that you were writing to spec and if you did not, they can't use any of your function. They have to go back and write the function correctly from scratch. It is as if you did not exist! So this is not a fussy detail, but a real requirement. Don't get creative with methods. Write to spec.