Study guide: Organize Programs into Clear Responsibilities
Design a paper helper for doubling a nonnegative integer count. State its contract, distinguish return from printing and give a valid example without modifying shared state.
Ideas to explain in your own words
- A function can return a value to its caller, print a visible message, do both, or do neither. A printed number is not automatically the function’s returned value. In Python a function that reaches the end without an explicit return returns None. These paper descriptions state exactly what each function does.
- Local variables belong to a function call’s own work under the stated model. A program can also use shared state, but hidden shared changes make reasoning harder. A pure calculation returns a result from its inputs without changing external state. Scope must be explicit before tracing a result.
- Decomposition divides a larger task into smaller responsibilities. A helper with a clear input, output and purpose can be checked separately. Combining unrelated tasks can hide dependencies. In this paper design, the goal is to keep validation, calculation and presentation as distinguishable steps.
- A function contract states accepted inputs, produced outputs and important conditions. Documentation helps a caller use a helper without guessing implementation details. Our model describes valid inputs and no outside effects. A contract also identifies what is not promised.
Useful vocabulary
- return
- Send a result back to the caller.
- local variable
- A value scoped to the stated call’s work.
- contract
- The accepted inputs and promised behavior of a helper.
Watch your reasoning
- Do not substitute printed text for the return value.
- Two functions using the same local name do not automatically share one variable.
- A helper name does not prove the helper actually has one responsibility.
- An example is useful but does not replace an input rule.
A worked review example
Function returns 5 and prints nothing. Classify the stated function behavior. The supported answer is Returns only. Track the caller’s returned value separately from visible output. Do not substitute printed text for the return value. A function can return a value to its caller, print a visible message, do both, or do neither. Accepts a nonnegative whole-number count. Classify this contract statement. The supported answer is Input requirement. Match the requested documentation element to its role in the contract. An example is useful but does not replace an input rule. A function contract states accepted inputs, produced outputs and important conditions. The supplied facts, rather than an assumed real-world detail, determine this paper case. Compare the recorded action or value with the stated rule before choosing an answer.
Make a practice plan
- Make a four-row summary, one row for each lesson, with its computing idea and one worked decision.
- Explain one tempting mistake using the actual supplied example.
- Complete the new unit review before consulting its explanations.
- Try the transfer task on paper and compare your evidence against the model response.