Constant and Default Parameters
- Review of Parameter Passing Schemes
- Call by Value (Copy Semantics)
- Call by Reference (Aliasing Semantics)
- Example: add.cpp
- Constant Parameters
- Useful in those situations where you want the efficiency of
call-by-reference, but the safety of call-by-value
- To implement use the keyword: const before the parameter
type.
- e.g.: const int &x
- Default Parameters
- Useful for when some of the parameters take typical values and only
sometimes take other values
- Example:
- To increment a variable x by 1, we can write: x = x +
1; or x += 1;
- As a special case of the common operation, C++ provides x++;or
++x;
- Likewise we can write add.cpp to increment by one, if no second
parameter is supplied
- Function Overloading
- Functions can be overloaded (given the same name), as long as the
compiler can distinguish the function based on the type and number of parameters
(e.g., see promptAndGet in add.cpp)