You've already seen, in the preceding example, the basic syntax that we need to use for pure functions in C++. You just need to remember the following four ideas:
- Pure functions don't have side effects; if they are part of a class, they can be static or const.
- Pure functions don't change their parameters, so every parameter has to be of the const, const&, or const* const type.
- Pure functions always return values. Technically, we can return a value through an output parameter, but it's usually simpler to just return a value. This means that pure functions usually don't have a void return type.
- None of the preceding points guarantee the lack of side effects or immutability, but they take us close. For example, data members can be marked as mutable and the const methods could change them.
We'll explore, in the following sections...