Practice question · Multiple choice
A function that reads no globals and changes nothing outside itself is called pure. Why do such functions make a program easier to reason about?
Hints
- To predict what an impure function returns, what else do you need to know?
- Ask which kind of function can be safely replaced by a lookup table of its past results.
Show the answer
D. Because their behaviour depends only on their arguments
Why
Everything a pure function does is in its arguments, so you can reason about it without holding the rest of the program in your head, and it can be memoised, parallelised or reordered for free. Concentrating side effects at the edges is how large systems stay tractable, whatever the paradigm.
Practise Functions and Modularity
The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.
More questions on Functions and Modularity
- Sort each change to a function by whether existing callers keep working untouched.
- If two different functions each define a local variable named count, changing one of them changes the other.
- Order what happens when the call f(3, 4) is evaluated.
- Extracting repeated code into a function usually makes a program longer, not shorter, once the definition and…