C41 · Topic 3.13 · 40 minutes · AAP-3.B, AAP-3.C
Writing a useful procedure
By the end you can:
- Specify inputs, result and preconditions.
- Combine sequencing, selection and iteration.
- Use parameters that actually affect behaviour.
Before this lesson: topics 3.8, 3.10, 3.12.
- Procedural abstraction
- Using a named procedure through its purpose/interface while hiding implementation details.
- Generalisation
- Replacing fixed choices with meaningful parameters so one definition serves multiple cases.
Understand
Develop a procedure from a clear contract: what inputs are allowed, what result is returned and what happens on boundaries or empty collections. Parameters should represent the differences between intended uses. Merely listing a parameter does not make the body general if it still uses a fixed constant instead.
A useful list procedure can sequence initialization, traverse each element, select qualifying values and return a result after the traversal. Write the contract first, then test calls showing that changing a relevant argument changes behaviour. Keep the empty-list result and inclusive/exclusive boundaries explicit.
Worked example
recommend(values,low,high)returns entries between low and high inclusive, requiring low≤high.
result ← []
FOR EACH value IN values
{ IF(value ≥ low AND value ≤ high) { APPEND(result,value) } }
RETURN(result)- For[1,5,8,5,10]and bounds 5…8,the result is[5,8,5].
- The same list with 8…10 returns[8,10].
- An empty list returns[]because initialization occurs before the loop.
Explore each concept
Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.
- One shared definition
- Modularity
- Ignored parameters and compatible calls
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- First caller threshold (0–10)3Waiting
- Second caller threshold (0–10)5Waiting
- ImplementationsharedWaiting
Ready to explore: Refactor copies and test meaningful parameters
Why: Change an input, predict the first effect, then advance one step.
Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 11,5,8,5,10Waiting
- Input 25Waiting
- Input 38Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Try repeated boundary values, no matches and an empty list.
Check a misconception
Claim: A parameter is meaningful even if the body overwrites or ignores it.
Changing only that argument must be able to change the relevant behaviour on some valid input.
Guided practice
Give two calls on one list demonstrating that high affects the result.
Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.
Independent transfer
Modify a minimum-only filter to use both inclusive bounds and explain each construct.
Exit check
Why state low≤high as a precondition?
Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.
Check yourself
Your practice so far
Check an answer to see its rationale.