- What is a pure function?
A pure function is a function that has two constraints, as follows:
-
- It always returns the same output values for the same argument values.
- It doesn't have side effects.
- How is immutability related to pure functions?
Pure functions are immutable because they don't change anything in the program state.
- How can you tell the compiler to prevent changes to a variable that's passed by value?
Simply define the parameter as const, as follows:
int square(const int value)
- How can you tell the compiler to prevent changes to a variable that's passed by reference?
Simply define the parameter as const&, as follows:
int square(const int& value)
- How can you tell the compiler to prevent changes to a pointer address that's passed by reference?
If the pointer is passed by value, nothing is needed since all the changes...