Practice question · Sort into groups
Sort each change to a function by whether existing callers keep working untouched.
Groups: Callers unaffected · Breaks callers
- Add a comment explaining the method
- Replace the body with a faster method giving the same results
- Add a required second parameter
- Rename a local variable inside the body
- Change the function to return nothing
Hints
- Only the interface is visible from outside, everything else is private.
- Which changes alter what a call must supply or what it gets back?
Show the answer
Callers unaffected: Rename a local variable inside the body, Replace the body with a faster method giving the same results, Add a comment explaining the method
Breaks callers: Add a required second parameter, Change the function to return nothing
Why
Internal names, internal methods and comments are invisible to callers; changing the parameters or the return value changes the interface itself and every call site must follow. Knowing which is which is what makes modular code safe to improve.
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
- A function that reads no globals and changes nothing outside itself is called pure. Why do such functions…
- 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…