Skip to main content

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.

AP CSP pseudocode
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.

Two procedures that look similar
ProcedureCallWhat the caller gets
PROCEDURE double(n) { RETURN(n * 2) }x ← double(5) + 1x 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.

Before: two copies
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.

After: one procedure, two calls
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)
Tracing calls with caps ← [0, 5, 2, 8]
CallValues countedReturns
countAvailable(caps, 3)5, 82
countAvailable(caps, 2)5, 2, 83
countAvailable([3, 3], 3)3, 32
countAvailable([], 3)none — the loop body never runs0
The same procedure in Python
def count_available(caps, minimum):    count = 0    for c in caps:        if c >= minimum:            count = count + 1    return count

A 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.

An ignored parameter
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
Open this example on its own

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

Ready to explore: Call, return, display and resume
  1. Argument (0–10)3Waiting
  2. 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

Ready to experiment
  1. Input 15Waiting
  2. Input 22Waiting
  3. 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

Ready to experiment
  1. Input 11,5,8,5,10Waiting
  2. Input 25Waiting
  3. 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.

AP CSP pseudocode
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)
  1. For each call, write which value each parameter receives.
  2. Trace price through each call and write the returned value.
  3. State what DISPLAY(a + b) shows.
  4. 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.

  1. Write countAvailable(caps, minimum) in AP pseudocode or Python.
  2. Call it twice on [0, 5, 2, 8] with minimums 3 and 2 and give both results.
  3. Explain, using those two results, how the minimum parameter affects the result.
  4. Give the result for an empty list and explain why.
  5. Explain one advantage of one shared procedure over two copies, using the room bug as your example.

Exit check

  1. Call countAvailable([4, 4, 1], minimum) with two different minimums that give different results, and explain the difference.
  2. 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

  1. Question 1 of 4For the call fee(4, 3), which parameter receives which value?

    Select one answer.

    AP CSP pseudocode
    PROCEDURE fee(members, rate){  RETURN(members * rate)}cost ← fee(4, 3)
  2. Question 2 of 4What does this program display?

    Select one answer.

    AP CSP pseudocode
    PROCEDURE bonus(points){  IF (points > 10)  {    RETURN(points + 5)  }  RETURN(points)}x ← bonus(12)y ← bonus(8)DISPLAY(x + y)
  3. Question 3 of 4double(n) ends with RETURN(n * 2). showDouble(n) ends with DISPLAY(n * 2). Which statement is true?

    Select one answer.

  4. Question 4 of 4Which version of countAvailable(caps, minimum) uses the minimum parameter to decide which capacities are counted?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Apply it somewhere new

  1. Question 1 of 4A club page and a room page both call countAvailable. A bug in the procedure is fixed. What is the effect?

    Select one answer.

  2. Question 2 of 4Two copies of a counting loop differ only in the minimum, 3 or 5. A bug is fixed in one copy only. Which change best prevents this kind of bug?

    Select one answer.

  3. Question 3 of 4Specification: countAvailable(caps, minimum) returns how many values in caps are at least minimum, and returns 0 for an empty list. Which two procedures meet the specification? Select two.

    Select exactly two answers (0 of 2 selected).

  4. Question 4 of 4countOver3(caps) returned the number of values of at least 3. It is replaced by countAvailable(caps, minimum). Which call keeps the old behaviour exactly?

    Select one answer.

Your practice so far

Check an answer to see its rationale.