9.5. const
parameters¶
You might have noticed that the parameters for after
and addTime
are being passed by reference. Since these are pure functions, they do
not modify the parameters they receive, so I could just as well have
passed them by value.
The advantage of passing by value is that the calling function and the callee are appropriately encapsulated—it is not possible for a change in one to affect the other, except by affecting the return value.
On the other hand, passing by reference usually is more efficient,
because it avoids copying the argument. Furthermore, there is a nice
feature in C++, called const
, that can make reference parameters
just as safe as value parameters.
If you are writing a function and you do not intend to modify a parameter, you can declare that it is a constant reference parameter. The syntax looks like this:
void printTime (const Time& time) ...
Time addTime (const Time& t1, const Time& t2) ...
I’ve included only the first line of the functions. If you tell the compiler that you don’t intend to change a parameter, it can help remind you. If you try to change one, you should get a compiler error, or at least a warning.
-
Q-1: Match the action to a benefit of its use.
Try again.
- passing by value
- the calling function and the callee are appropriately encapsulated—it is not possible for a change in one to affect the other, except by affecting the return value.
- passing by reference
- more efficient, because it avoids copying the argument
- Makes reference parameters just as safe as value parameters.
- This is true.
- Can be used when you intend to modify a parameter.
- It is used when you are writing a function and you do not intend to modify a parameter.
- The syntax can look like this: void printTime (const Time& time)...
- This is true
Q-2: Which statement does NOT describe the const
as a parameter feature in C++?