Presentation is loading. Please wait.

Presentation is loading. Please wait.

Default values of parameters

Similar presentations


Presentation on theme: "Default values of parameters"— Presentation transcript:

1 Default values of parameters

2 Outline In this lesson, we will:
How parameters can be given default values Learn the rules under which they can be defined and used Cover some of the more common errors

3 Introduction Up to this point, every parameter must be given a value
In some cases, some values may have obvious default values Such parameters need only be specified if they differ from the default Benefit: cleaner function calls Issue: the programmer reading the code must be aware of the default values These are also called default arguments or optional parameters

4 Declaring default values
Default values are always given in the function declaration Never in the definitions The default values should usually be obvious given the type Occasionally, values like -1 or nan are used to indicate the user has not supplied a default value Type Usual default value int double 0.0 bool false char '\0' std::string ""

5 Declaring default values
Example: The fast_sin function assumes the arguments are in radians We my give the user the option of specifying the argument in degrees As , there are radians per degree double my_sin( double x, bool using_degrees = false ); double my_sin( double x, bool using_degrees ) { double const RADIANS_PER_DEGREE{ }; if ( using_degrees ) { x *= RADIANS_PER_DEGREE; } // The rest of the fast sine algorithm

6 ✘ ✔ ✔ ✘ Using default values
If a parameter has a default value, all subsequent parameters must, too, have default values If you want to specify a non-default value for a specific parameter, you must also provide arguments for all parameters up to that value, regardless as to whether or not they are already default values void f( int n, int m = 0 ); void h( int n = 0, int m ); void g( int n = 0, double m = 0.0 ); g(); g( 42 ); g( 0, 91.0 ); g( , 91.0 ) g( 91.0 ) // same as g( 91 )

7 Error messages In some cases, the error messages can be made quite clear: If the declaration is double add( double x = 0.0, double y ); The error message is example.cpp: In function 'double add(double, double)': example.cpp:4:5: error: default argument missing for parameter 2 of 'double add(double, double)' double add( double x = 0.0, double y ); ^

8 Error messages In other cases, it could be more useful
If the declaration is double add( double x, double y, double z = 0.0 ); The error message when calling add( 42.0 ) does not suggest the default value of the third parameter: example.cpp: In function 'double add(double, double)': example.cpp:11:11: error: too few arguments to function 'double add(double, double, double)' add( 42.0 ); ^ example.cpp:6:8: note: declared here double add( double x, double y, double z ) {

9 Error messages Suppose you supplied the default value in both the functionxxx declaration and definition: example.cpp: In function 'double add(double, double, double)': example.cpp:6:48: error: default argument given for parameter 3 of 'double add(double, double, double)' double add( double x, double y, double z = 0.0 ) { ^ example.cpp:4:8: error: after previous specification in 'double add(double, double, double)' double add( double x, double y, double z = 0.0 );

10 Summary Following this lesson, you now:
Understand the use and application of default values for parameters Know the syntax: All parameters after the first with a default value must have a default value Default values cannot be skipped The default values should be the obvious values Know the error messages are reasonably straight-forward

11 References [1] No references?

12 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

13 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Default values of parameters"

Similar presentations


Ads by Google