|
| 1 | +# Functions: default argument |
| 2 | +_Skeleton instructions are typeset in italic text._ |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +Functions in C++ may be overloaded with different numbers and types of |
| 7 | +parameters. It may be of value to specify default arguments for some number |
| 8 | +of parameters, to allow a caller to avoid specifying arguments that |
| 9 | +rarely change, or to enable expanding the set of parameters while |
| 10 | +maintaining backward compatibility with existing callers. |
| 11 | + |
| 12 | +<table> |
| 13 | + <thead> |
| 14 | + <th>Level</th> |
| 15 | + <th>Objectives</th> |
| 16 | + </thead> |
| 17 | + <tr> |
| 18 | + <td>Foundational</td> |
| 19 | + <td>Define and use functions with default arguments</td> |
| 20 | + </tr> |
| 21 | + <tr> |
| 22 | + <td>Main</td> |
| 23 | + <td></td> |
| 24 | + </tr> |
| 25 | + <tr> |
| 26 | + <td>Advanced</td> |
| 27 | + <td>refinement of default arguments through multiple declarations</td> |
| 28 | + </tr> |
| 29 | +</table> |
| 30 | + |
| 31 | +## Motivation |
| 32 | + |
| 33 | +Default arguments allow the omission of arguments with obvious or common |
| 34 | +values. Also may be utilized to extend an existing function signature |
| 35 | +without forcing changes to existing calling code. |
| 36 | + |
| 37 | +## Topic introduction |
| 38 | + |
| 39 | +Explain how default arguments work and how to define them. |
| 40 | + |
| 41 | +## Foundational: Using and defining functions with default arguments |
| 42 | + |
| 43 | +### Background/Required Knowledge |
| 44 | + |
| 45 | +A student is able to: |
| 46 | + |
| 47 | +* Make calls to existing functions, passing arguments [[Functions: calling functions]][1] |
| 48 | +* Declare member and non-member functions, separate from definitions |
| 49 | +* Define member and non-member functions [[Functions: member functions]][2] |
| 50 | +* Explain what a default constructor is and does [[C++ object model: constructors]][3] |
| 51 | + |
| 52 | +### Student outcomes |
| 53 | + |
| 54 | +A student should be able to: |
| 55 | + |
| 56 | +1. Call to a function with a default argument with or without that argument specified |
| 57 | +2. Declare a function with a default argument, and omit the default in the definition's signature |
| 58 | +3. Explain when the lifetime of a default argument begins and ends |
| 59 | + |
| 60 | + |
| 61 | +### Caveats |
| 62 | + |
| 63 | +* When no forward-declaration exists, the definition serves as the declaration |
| 64 | +* When multiple declarations exist, only one may specify the default for any particular parameter, but multiple declarations may specify the defaults for different parameters. |
| 65 | +* Additional default values may be specified for other parameters in repeat declarations |
| 66 | +* Calling an overloaded function with fewer arguments may be ambiguous with regard to an overload with default arguments |
| 67 | + |
| 68 | +### Points to cover |
| 69 | + |
| 70 | +* Default value may only be specified once for each parameter among all declarations |
| 71 | +* Default values must start from the rightmost parameter and continue leftward without gaps |
| 72 | +* Considerations of when to use default arguments vs overload set |
| 73 | + |
| 74 | +## Main: implementing * |
| 75 | + |
| 76 | +### Background/required knowledge |
| 77 | + |
| 78 | +* All of the above. |
| 79 | + |
| 80 | +### Student outcomes |
| 81 | + |
| 82 | +A student should be able to: |
| 83 | + |
| 84 | +### Caveats |
| 85 | + |
| 86 | + |
| 87 | +### Points to cover |
| 88 | + |
| 89 | +## Advanced |
| 90 | + |
| 91 | +Subsequent redeclarations of the same function may add default argument |
| 92 | +values, which are then usable by callers. |
| 93 | +Though a single parameter cannot be given a default argument twice in the same |
| 94 | +translation unit, it is legal, though ill-advised, to give the same |
| 95 | +function different default arguments in different translation units. |
| 96 | + |
| 97 | + |
| 98 | +[1]: ../functions/calling-functions.md |
| 99 | +[2]: ../functions/member-functions.md |
| 100 | +[3]: ../object-model/constructors.md |
0 commit comments