A function is considered a pure function if the result depends only on the arguments, and it has no side effects like modifying an argument or outputting something. The only result of calling a pure function is the return value.
Listing9.4.1.One example is the function after, which compares two Times and returns a bool that indicates whether the first operand comes after the second. Take a look at this active code.
What is the result of this function if the two times are equal? Does that seem like the appropriate result for this function? If you were writing the documentation for this function, would you mention that case specifically?
A second example is addTime, which calculates the sum of two times. For example, if it is 9:14:30, and your breadmaker takes 3 hours and 35 minutes, you could use addTime to figure out when the bread will be done.
Listing9.4.2.Take a look at this active code. If currentTime contains the current time and breadTime contains the amount of time it takes for your breadmaker to make bread, then you could use addTime to figure out when the bread will be done.
The problem is that this function does not deal with cases where the number of seconds or minutes adds up to more than 60. When that happens we have to βcarryβ the extra seconds into the minutes column, or extra minutes into the hours column.
This code demonstrates two operators we have not seen before, += and -=. These operators provide a concise way to increment and decrement variables. For example, the statement sum.second -= 60.0; is equivalent to sum.second = sum.second - 60;