Default Values for Parameters

To make a function as general as possible, you need to specify all its parameters. It is often a bad idea to make a function's result depend on some global parameter, and it is frustrating to use a function that depends on some mysterious constants.

Specifying too many parameters as arguments is clumsy and makes the function's use hard to remember. In Chapter 5, “Structures and Pointers,” you saw one solution—to wrap up the parameters as members of a structure, which could then be efficiently passed by reference to a function. This solution is the equivalent of the named parameters that some languages support. Here is an example of what this kind of parameter passing looks like. do_operation() is a function with a large number of parameters, but we want to change only the font used for text output. Instead of passing a dozen or more parameters, only the font properties are changed. Again, this technique can be clumsy because you usually need to initialize the other members of the structure with some default values.

;> OperationArgs args;
;> args.flags = SET_FONT;
;> args.fontsize = 12;
;> args.fontname = "arial";
;> do_operation(args);
					

You can use default arguments to handle this problem. For instance, here is a function that prints a little table of sine and cosine values to some output stream. Notice that the last argument dx of dump_trig() is declared like an initialized variable (double dx = 0.1). Any arguments that specify initial values in this way are called default arguments. Such arguments may be left out of function calls.


;> void dump_trig(ostream& out,
;>      double x1, double x2, double dx = 0.1)
;> {
						for(double x = x1; x < x2; x+=dx)
;2}     out << sin(x) << ' ' << cos(x) << endl;
;1}  }
;> dump_trig(cout,0.0,0.5);  // only out, x1, and x2 specified!
0.000000 1.000000
0.099833 0.995004
0.198669 0.980067
0.295520 0.955336
0.389418 0.921061

In this example, the function dump_trig() defines a sensible default value for the gap between x values dx. If you choose not to use the last argument, a value of 0.1 will be assumed, or you can specify all four arguments.

In the previous chapter, you defined the make_point() function to construct Point objects. Here it is redefined to use default arguments:

Point make_point(int x=0, int y=0) {
   Point p;
   p.x = x;  p.y = y;
   return p;
 }
;> Point p1 = make_point();
;> p1.x;  p1.y;
(int) 0
(int) 0

In this example you can specify two, one, or no arguments for make_point(). It is important to have sensible defaults that would be obvious (for example, setting the point coordinates to (10,10) would not be obvious). Zero is a good obvious value for many types.

NOTE

It is an error to redefine default arguments in the function definition if the function definition has already been declared with default values. So, if your function is defined in a header, keep the default values there.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.15