Pilot · L06 · 60 minutes
Procedures, parameters and reuse
Trace procedure calls by matching arguments to parameters, distinguish a returned result from displayed output, and refactor two copies of a counting loop into countAvailable(caps, minimum) whose parameter really changes its result.
Topics: 3.12 Calling Procedures · 3.13 Developing Procedures
Goals and vocabulary
By the end you can:
- Match arguments to parameters by position and trace a call that returns a value.
- Explain the difference between returning a value and displaying it.
- Refactor duplicated code into one procedure with parameters for what differs.
- Show with two calls that a parameter changes a procedure’s result, and recognise an ignored parameter.
- Choose a generalisation that preserves existing behaviour.
- Procedure
- A named group of instructions that can take parameters and return a value.
- Parameter
- A variable in a procedure’s definition that receives a value when the procedure is called.
- Argument
- The value passed into a parameter in a particular call.
- Return value
- The value a procedure gives back to its caller with RETURN.
- Procedural abstraction
- Using a procedure by what it does, without needing to know how it does it.
- Generalise
- Replace a fixed value with a parameter so one procedure handles many cases.
Calling a procedure with arguments
A procedure is a named group of instructions that may take parameters and may return a value. When you call it, each argument you pass is matched to a parameter by position: the first argument goes to the first parameter, the second to the second.
PROCEDURE fee(members, rate){ RETURN(members * rate)}cost ← fee(4, 3)In the call fee(4, 3), members gets 4 and rate gets 3. The procedure returns 12, and cost ← 12. Swapping the arguments would swap which parameter gets which value.
Returning a value versus displaying it
RETURN(value) ends the procedure and gives value back to the caller, which can store it or use it in an expression. DISPLAY(value) shows it to the user but gives nothing back.
| Procedure | Call | What the caller gets |
|---|---|---|
| PROCEDURE double(n) { RETURN(n * 2) } | x ← double(5) + 1 | x is 11. The 10 was returned and used. |
| PROCEDURE showDouble(n) { DISPLAY(n * 2) } | showDouble(5) | 10 appears on screen. No value comes back to use in a calculation. |
If a later step needs the result — to add, compare or store it — the procedure must RETURN it.
Worked example: refactoring duplicated counting
Worked example
The club page and the room page each count capacities of at least some minimum. The code was copied, then one copy was fixed and the other was not.
clubCount ← 0FOR EACH c IN clubCaps{ IF (c ≥ 3) { clubCount ← clubCount + 1 }}roomCount ← 0FOR EACH r IN roomCaps{ IF (r > 5) { roomCount ← roomCount + 1 }}The room copy uses > where the room rule says “at least 5”. Bugs like this appear because each copy must be fixed separately. Replace both with one procedure whose parameters carry what differs: the list and the minimum.
PROCEDURE countAvailable(caps, minimum){ count ← 0 FOR EACH c IN caps { IF (c ≥ minimum) { count ← count + 1 } } RETURN(count)}clubCount ← countAvailable(clubCaps, 3)roomCount ← countAvailable(roomCaps, 5)| Call | Values counted | Returns |
|---|---|---|
| countAvailable(caps, 3) | 5, 8 | 2 |
| countAvailable(caps, 2) | 5, 2, 8 | 3 |
| countAvailable([3, 3], 3) | 3, 3 | 2 |
| countAvailable([], 3) | none — the loop body never runs | 0 |
def count_available(caps, minimum): count = 0 for c in caps: if c >= minimum: count = count + 1 return countA parameter must change the behaviour
A procedure generalises behaviour only when its parameters are actually used. If countAvailable(caps, minimum) still compares with a fixed 3 inside, the minimum parameter is ignored: changing only minimum cannot change which values are counted. The procedure is not general with respect to that threshold.
PROCEDURE countAvailable(caps, minimum){ count ← 0 FOR EACH c IN caps { IF (c ≥ 3) { count ← count + 1 } } RETURN(count)}Why one shared procedure is better
- One fix repairs every caller. Fixing countAvailable fixes the club page and the room page at once.
- Callers can be read at a higher level: countAvailable(roomCaps, 5) says what happens without showing how.
- New callers reuse tested code instead of copying it.
When you generalise an existing procedure, check that the old behaviour is still available. If the old countOver3(caps) counted values of at least 3, then countAvailable(caps, 3) must give exactly the same results.
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.
- Argument binding and local calls
- RETURN ends the call
- DISPLAY is not a returned value
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
- Argument (0–10)3Waiting
- Procedure bodyreturnWaiting
Ready to explore: Call, return, display and resume
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 15Waiting
- Input 22Waiting
- Input 33Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Change one input, predict the difference, then reset and compare.
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.
Common misconceptions
Displaying is the same as returning
The belief: If the procedure shows the answer, the caller can use it.
What is actually true: DISPLAY shows a value to the user; only RETURN gives it back for the caller to store or calculate with.
Check yourself: Could you write x ← procedure(…) + 1 and get a number?
Having a parameter makes a procedure general
The belief: countAvailable(caps, minimum) is general because it has a minimum parameter.
What is actually true: The parameter must be used in the body. If minimum is ignored, changing only that argument cannot change the result.
Check yourself: Do two calls with different minimums ever give different results?
Arguments match parameters by name
The belief: The argument called minimum goes into the parameter called minimum wherever it is.
What is actually true: Arguments are matched by position, not by name.
Check yourself: Which parameter is first in the definition, and which argument is first in the call?
Guided practice
Guided: predict two calls
With a partner, predict each call before tracing it.
PROCEDURE ticketPrice(age, member){ price ← 8 IF (age < 12) { price ← 5 } IF (member) { price ← price - 2 } RETURN(price)}a ← ticketPrice(10, true)b ← ticketPrice(15, false)DISPLAY(a + b)- For each call, write which value each parameter receives.
- Trace price through each call and write the returned value.
- State what DISPLAY(a + b) shows.
- Rewrite ticketPrice so it displays the price instead of returning it, and explain why DISPLAY(a + b) would then fail.
Independent practice
Independent: refactor and test
Replace the two copies in the worked example with your own countAvailable procedure.
- Write countAvailable(caps, minimum) in AP pseudocode or Python.
- Call it twice on [0, 5, 2, 8] with minimums 3 and 2 and give both results.
- Explain, using those two results, how the minimum parameter affects the result.
- Give the result for an empty list and explain why.
- Explain one advantage of one shared procedure over two copies, using the room bug as your example.
Exit check
- Call countAvailable([4, 4, 1], minimum) with two different minimums that give different results, and explain the difference.
- Explain why a procedure that ends with DISPLAY(count) cannot be used in total ← countAvailable(a, 3) + countAvailable(b, 3).
Check yourself
Practice only: answers are checked in your browser and nothing is saved or sent to your teacher. Each explanation links back to the section it practises.
Lesson practice
Your practice so far
Check an answer to see its rationale.
Apply it somewhere new
Your practice so far
Check an answer to see its rationale.