Default values of parameters

Slides:



Advertisements
Similar presentations
Recursive binary search
Advertisements

For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
The structured programming theorem
Pointers.
Anatomy of a program.
Switch statement.
Do-while loops.
Command-line arguments
Console input.
This.
Sorted arrays.
Floating-point primitive data types
Dynamically allocating arrays within structures
Break statements.
Keywords.
Linked Lists.
Logical operators.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The call stack and recursion and parameters revisited
The ternary conditional operator
Dynamically allocating structures
Memory leaks.
Pushing at the back.
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Templated Linked Lists
Polymorphism.
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Code-development strategies
Throwing exceptions.
Anatomy of a program.
Selection sort.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Recursive functions.
Class variables and class functions
Operator overloading.
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
The structured programming theorem
Recursive functions.
Algorithms and templates
Presentation transcript:

Default values of parameters

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

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

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 ""

Prescribing a default behavior 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{0.017453292519943296}; if ( using_degrees ) { x *= RADIANS_PER_DEGREE; } // The rest of the fast sine algorithm

The factorial function Example: The recursive factorial function does not lend itself to tail recursion unsigned long factorial( unsigned long n ); unsigned long factorial( unsigned long n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } The result is used in an arithmetic expression

The factorial function The factorial function can be rewritten to make use of tail recursion The return statement is a recursive function call unsigned long factorial( unsigned long n, unsigned long result = 1 ); unsigned long result ) { if ( n <= 1 ) { return result; } else { return factorial( n - 1, n*result ); } The result is a recursive function call to factorial(…)

✘ ✔ ✔ ✘ 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 x = 0.0 ); g(); g( 42 ); g( 0, 91.0 ); ✔ ✘ g( , 91.0 ) g( 91.0 ) // same as g( 91 )

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 ); ^

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 ) {

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 );

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

References [1] No references?

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 https://www.rbg.ca/ for more information.

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.