A procedure is a named block of code that runs when you call it. Its parameters receive the values of the arguments in the call, matched by position, and RETURN sends a value back to the line that made the call and ends the procedure at once. In AP® Computer Science Principles you trace a call by writing down each parameter's value, running the body, and putting the returned value where the call was.
Quick check
What does IF(x > 3) { x ← 0 } ELSE { x ← x + 1 } do when x is 3?Show answerHide answer
It runs the
ELSEblock, because 3 > 3 is false, soxbecomes 4.After a ← 4 and b ← a * 2, what is b?Show answerHide answer
- The right-hand side is worked out with
aequal to 4, then stored inb.
- The right-hand side is worked out with
A procedure ends with RETURN(total). Where does the value of total go?Show answerHide answer
Back to the line that called the procedure, where it can be stored, displayed or used in an expression.
Know these already? Jump to the practice questions
What you'll be able to do
- Identify the parameters, arguments and return value in a procedure call.
- Predict what a call returns by matching arguments to parameters in order.
- Explain why the lines after a
RETURNthat runs are skipped. - Use a trace table to follow a procedure called inside a loop.
Key terms
Procedure
A named group of instructions that may take parameters and may return a value.
- In plain English
- A mini-program with a name that you can run whenever you need it.
- Don't write
- "A procedure runs as soon as it is defined."
Parameter
A variable in the procedure's header that receives a value when the procedure is called.
- In plain English
- An empty slot the call fills in.
- Don't write
- "A parameter is the value you pass in."
Argument
The value placed in a call's brackets, which is given to the matching parameter.
- In plain English
- What you put in the slot.
- Don't write
- "Arguments are matched to parameters by name."
Return value
The value a procedure sends back to its caller with RETURN.
- In plain English
- The answer the procedure hands back.
- Don't write
- "
RETURNdisplays the value."
How do you define and call a procedure?
You define a procedure once, and its body runs only when it is called.
PROCEDURE area(w, h)
{
RETURN(w * h)
}
rect ← area(3, 5)
DISPLAY(rect)
w and h are the parameters. In the call area(3, 5), 3 and 5 are the arguments: the first argument goes to the first parameter, so w is 3 and h is 5. The body works out 15 and returns it, so rect becomes 15 and the segment displays 15.
The same procedure works for any rectangle: area(2, 7) returns 14. The parameters are what make one procedure reusable. Defining procedures, calling them and RETURN are all on the exam reference sheet.
Check yourself
Show answerHide answer
w is 6 and h is 2, because arguments are matched to parameters in order.
What does RETURN do?
RETURN(expression) does two things: it sends the value back to the caller, and it ends the procedure straight away. Lines after a RETURN that runs are skipped.
PROCEDURE sign(n)
{
IF(n < 0)
{
RETURN(-1)
}
IF(n > 0)
{
RETURN(1)
}
RETURN(0)
}
sign(-7) returns -1 from the first RETURN and never reaches the others. sign(0) passes both tests and returns 0 from the last line.
The caller must use the returned value: store it (s ← sign(4)), display it (DISPLAY(sign(4))) or put it in an expression (sign(4) + 1). A call on a line by itself throws the value away. DISPLAY inside a procedure only shows a value; it doesn't hand anything back.
Key idea
RETURN hands back one value and ends the procedure immediately.
Check yourself
Show answerHide answer
sign(4)returns 1, and 1 × 3 = 3.
How do you trace a procedure called in a loop?
Each call starts fresh: the parameters get the new argument values, the body runs, and the returned value replaces the call. Trace with one row per call.
PROCEDURE square(n)
{
RETURN(n * n)
}
total ← 0
k ← 1
REPEAT 3 TIMES
{
total ← total + square(k)
k ← k + 1
}
DISPLAY(total)
| Pass | k (argument) | n (parameter) | Returned | total |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 4 | 5 |
| 3 | 3 | 3 | 9 | 14 |
The segment displays 14. Keep the argument column (the caller's variable) separate from the parameter column (the procedure's variable): they hold the same value here only because k is passed in.
Check yourself
Show answerHide answer
30: the fourth call returns 16, and 14 + 16 = 30.
Worked example 1: Trace two calls
Question: What is displayed when this code segment runs?
PROCEDURE price(qty, cost)
{
total ← qty * cost
IF(qty ≥ 10)
{
total ← total - 5
}
RETURN(total)
}
a ← price(4, 3)
b ← price(10, 2)
DISPLAY(a)
DISPLAY(b)
What tells you the method
Two calls with different arguments, so trace each call separately.
Step 1: Trace
price(4, 3).qtyis 4 andcostis 3, sototalis 12. 4 ≥ 10 is false, so the procedure returns 12 andais 12.WhyArguments match parameters in order: 4 goes to
qty, 3 tocost.Step 2: Trace
price(10, 2).totalis 20. 10 ≥ 10 is true, sototalbecomes 15, which is returned tob.WhyThe second call starts fresh with its own argument values.
Step 3: Read the displays.
12 and 15, on one line.
WhyEach
DISPLAYshows its value followed by a space.
Check it
qty = 10 is the boundary. price(9, 2) gives 18 with no discount, so the discount starts exactly at 10, as ≥ says.
Answer
12 15
Worked example 2: Design a procedure
Question: A game keeps a value between a lower and an upper limit: below the range it becomes the lower limit, above the range it becomes the upper limit, otherwise it stays the same. Design a procedure clamp, then state what clamp(15, 0, 10) returns.
What tells you the method
A design task: name the inputs (parameters), the output (return value) and the steps, then test by tracing.
Step 1: State the input, output and steps.
Inputs:
value,lowandhigh. Output: the adjusted value. Steps: ifvalueis belowlow, returnlow; if it is abovehigh, returnhigh; otherwise returnvalue.WhyThree parameters let one procedure work for any range.
Step 2: Write the procedure.
PROCEDURE clamp(value, low, high) { IF(value < low) { RETURN(low) } IF(value > high) { RETURN(high) } RETURN(value) }WhyEach
RETURNends the procedure, so noELSEis needed.Step 3: Trace
clamp(15, 0, 10).15 < 0 is false; 15 > 10 is true, so it returns 10.
WhyTracing on a stated input tests the design.
Check it
clamp(-3, 0, 10) returns 0 from the first test, and the boundary clamp(10, 0, 10) fails both tests and returns 10, unchanged.
Answer
10
Worked example 3: A procedure inside a loop
Question: What is displayed when this code segment runs?
PROCEDURE step(n)
{
IF(n MOD 2 = 0)
{
RETURN(n / 2)
}
RETURN(3 * n + 1)
}
x ← 6
count ← 0
REPEAT UNTIL(x = 1)
{
x ← step(x)
count ← count + 1
}
DISPLAY(count)
- A) 8
- B) 7
- C) 9
- D) Nothing: the loop never stops.
What tells you the method
A call inside a loop, so trace one row per call.
Step 1: Trace the calls.
Even values are halved and odd values become 3n + 1: 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1.
WhyThe returned value replaces
x, so each call's result is the next call's argument.Step 2: Count the calls.
There are 8 arrows, so
countis 8 whenxreaches 1.Whycountgoes up once per call, and the loop checksx = 1before each pass.
Check it
The last call is step(2), which returns 1; the loop then checks x = 1 and stops, so there is no ninth call.
Answer
A
Your turn
Question: What is displayed when this code segment runs?
PROCEDURE diff(a, b)
{
RETURN(a - b)
}
x ← 10
y ← 4
DISPLAY(diff(y, x))
DISPLAY(diff(x, diff(x, y)))
Step 1: The first call.
diff(y, x)givesathe value 4 andbthe value 10, so it returns 4 − 10 = -6.Step 2: Your go: work out the inner call first, then the outer call.
Show the full solutionHide the solution
Step 2. The inner call diff(x, y) returns 10 − 4 = 6. The outer call is then diff(10, 6), which returns 4. The displays show -6 and 4 on one line.
Answer: -6 4
Traps
Trap
Arguments are matched to parameters by their names.
It seems right because variables are often named after the parameters. But matching is by position: the first argument goes to the first parameter, whatever the names.
PROCEDURE f(a, b) returns a - b. With a ← 2 and b ← 9, what does f(b, a) return?
- Parameter
areceives 9 and parameterbreceives 2.
Trap
The lines after a RETURN still run.
It seems right because code normally runs to the end of its block. But RETURN leaves the procedure at once, so later lines in it are skipped.
PROCEDURE g(n) has RETURN(n * 2) as its first line and DISPLAY(n) as its second. What does DISPLAY(g(4)) show?
8 only. DISPLAY(n) inside g never runs.
Trap
Calling double(x) on its own line doubles x.
It seems right because the call uses x. But the procedure returns a new value and the line throws it away; write x ← double(x) to keep it.
double(n) returns 2 * n. After x ← 5 and then double(x) on its own line, what is x?
- The returned 10 was never stored.
What's examined
On the AP® Computer Science Principles exam, procedures are part of Big Idea 3: Algorithms and Programming. Multiple-choice questions ask what a call returns or displays and which code completes a procedure. Your Create program must include a procedure you wrote, with at least one parameter, that is called in your program; the written-response questions ask about your own code. Why procedures manage complexity is in the note on procedural abstraction, libraries and APIs.
Practice questions
- Recall
PROCEDURE area(w, h)is called asarea(3, 5). Which are the arguments?
- A)
wandh - B) 3 and 5
- C)
area - D) 15
Show answer
Answer: B. Arguments are the values in the call's brackets.
Why the others are wrong:
- A names the parameters, which receive the arguments.
- C is the procedure's name.
- D is the return value.
- Apply What is displayed when this code segment runs?
PROCEDURE grade(score)
{
IF(score ≥ 90)
{
RETURN(4)
}
IF(score ≥ 80)
{
RETURN(3)
}
RETURN(2)
}
DISPLAY(grade(85))
DISPLAY(grade(90))
DISPLAY(grade(79))
Show answer
Answer: 3 4 2
Marking points:
- 85 fails the first test and passes the second: 3.
- 90 passes the first test (the boundary): 4.
- 79 fails both tests and reaches the last line: 2.
- Apply
PROCEDURE addUp(a, b)contains onlyRETURN(a + b * 2). What value doesaddUp(addUp(1, 2), 3)return?
Show answer
Answer: 11
Marking points:
- The inner call returns 1 + 2 × 2 = 5.
- The outer call is
addUp(5, 3), which returns 5 + 3 × 2 = 11.
- Exam-style
PROCEDURE larger(a, b)should return the larger of its two arguments. Which code can be its body?
- A)
IF(a > b) { RETURN(b) }, thenRETURN(a) - B)
IF(a > b) { RETURN(a) }, thenRETURN(a) - C)
IF(a > b) { RETURN(a) }and nothing else - D)
IF(a > b) { RETURN(a) }, thenRETURN(b)
Show answer
Answer: D. If a is larger it returns a; otherwise it returns b.
Why the others are wrong:
- A returns the wrong variable in each case, so it gives the smaller value.
- B returns
ain both cases, solarger(3, 8)returns 3. - C has no
RETURNthat runs whenbis larger, sobis never handed back.
- Exam-style The procedure should return 1 + 2 + … + n;
sumTo(4)should return 10. Give the number of the line that stops it working.
1: PROCEDURE sumTo(n)
2: {
3: total ← 0
4: k ← 1
5: REPEAT n TIMES
6: {
7: total ← total + k
8: k ← k + 1
9: RETURN(total)
10: }
11: }
Show answer
Answer: Line 9.
Marking points:
RETURNinside the loop ends the procedure after the first pass, sosumTo(4)returns 1.- Moving
RETURN(total)after the loop makes it return 10.
- Apply What value does
totalhold after this code segment runs?
PROCEDURE bonus(p)
{
IF(p > 50)
{
RETURN(p + 10)
}
RETURN(p)
}
total ← 0
k ← 40
REPEAT 3 TIMES
{
total ← total + bonus(k)
k ← k + 10
}
Show answer
Answer: 160
Marking points:
- The calls are
bonus(40),bonus(50)andbonus(60), which return 40, 50 and 70. - 50 > 50 is false, so 50 gets no bonus; 40 + 50 + 70 = 160.
- Exam-style What is displayed when this code segment runs?
PROCEDURE check(n)
{
IF(n > 5)
{
RETURN(n - 5)
}
IF(n > 2)
{
RETURN(n * 2)
}
RETURN(n)
}
DISPLAY(check(7))
DISPLAY(check(4))
DISPLAY(check(1))
- A)
14 8 1 - B)
7 4 1 - C)
2 8 1 - D)
2 8
Show answer
Answer: C. check(7) returns 2 from the first RETURN; check(4) returns 8; check(1) reaches the last line and returns 1.
Why the others are wrong:
- A lets
check(7)carry on past its firstRETURNto the second. - B displays the arguments instead of the returned values.
- D misses the final
RETURN(n), which runs when both tests fail.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- A procedure's body runs only when it is called.
- Arguments go to parameters in order, never by name.
RETURNhands back one value and ends the procedure at once.- Store, display or use a returned value, or it is lost.
- Trace calls in a loop with one row per call: argument, parameter, returned value.