BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.12
3.12 Calling Procedures
Calling a procedure hands control to it, possibly with values, and gets control (and maybe a result) back. The vocabulary here is precise and tested.
What you need to know
- A procedure is a named group of instructions that can be executed by calling its name. Other languages call these functions or methods.
- Parameters are the variables listed in the procedure's definition. Arguments are the actual values passed in when it's called. The first argument goes into the first parameter, and so on.
- When a procedure is called, the program jumps into the procedure, runs it, and then returns to the line after the call. That interrupts normal sequential flow.
RETURN (expression)ends the procedure immediately and sends the value back to the caller. Code after a RETURN in the same path never runs.- A procedure with a RETURN can be used inside an expression:
x ← square(4) + 1. A procedure without a RETURN (one that just DISPLAYs or changes things) is called as a statement. - The same procedure can be called many times with different arguments — that's the point.
- Modularity: breaking a program into procedures makes each piece easier to write, test, and reuse.
Worked example
PROCEDURE double(n)
{
RETURN (n * 2)
}
a ← 3
b ← double(a) + double(5)
DISPLAY(b)
double(a) passes the argument 3 into parameter n, returns 6. double(5) returns 10. b = 16. Output: 16. Note that a is still 3 — the procedure got a copy.
Trace it yourself
Step through with the buttons, or use the ← → keys. Changed variables are highlighted.
Exam tip: Track "where am I" during a trace. When you hit a procedure call, write down the line you'll return to, go execute the procedure with the arguments substituted for the parameters, and come back. RETURN means you stop reading the procedure instantly — even if there are lines below it.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- A procedure call interrupts sequential execution: the program remembers where it was, jumps to the procedure, runs it, then returns to the line after the call. If that line was in the middle of an expression, the returned value is substituted in and evaluation continues.
- Parameters are local to the procedure. They're created when it's called, initialized to the argument values, and gone when it returns. Changing a parameter inside the procedure doesn't change the caller's variable (for simple values).
- A procedure can have zero parameters (called with empty parentheses) or several. Arguments are matched to parameters by position, not name: the first argument fills the first parameter.
RETURNdoes two things: it provides the value the call evaluates to, and it ends the procedure immediately. Any code after a RETURN that executes is skipped. Multiple RETURNs in a procedure (one per branch) is normal; exactly one runs per call.- A procedure without a RETURN produces no value — it's called for its side effect (displaying something, modifying a list). Using such a call in an expression (
x ← show(5) + 1) is meaningless. - Procedures can call other procedures, and can call themselves (recursion isn't on the CSP exam, but nested calls are). Trace each call to completion before continuing the caller.
- The AP reference sheet defines the syntax:
PROCEDURE name(parameter1, parameter2) { instructions }andRETURN (expression). Block-based pseudocode draws the same thing as shapes.
Mistakes that cost points
- Continuing past a RETURN. Once RETURN executes, the procedure is done. Code below it on the same path never runs, even if it looks like it should.
- Losing your place. Write down the line number you'll return to before jumping into the procedure. Nested calls need a stack of these.
- Expecting the caller's variable to change.
double(a)doesn't change a. Only the returned value matters — and only if it's stored. - Matching arguments by name. Position is all that matters. If the procedure is
f(x, y)and you callf(y, x), the values are swapped.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Consider the following procedure.
PROCEDURE mystery(a, b)
{
IF (a > b)
{
RETURN (a - b)
}
RETURN (b - a)
}What is displayed by DISPLAY(mystery(4, 9))?Show answer
Answer: B. 4 > 9 is false, so the first RETURN is skipped. The second RETURN gives 9 - 4 = 5.
Q2 In the call
calculate(7, "kg") to a procedure defined as PROCEDURE calculate(amount, unit), which of the following is true?Show answer
Answer: B. Parameters are the names in the definition; arguments are the values in the call.
Key vocabulary
- Procedure
- a named block of code that can be called; also called a function or method
- Parameter
- a variable in a procedure's definition that receives a value when called
- Argument
- the actual value passed to a procedure when it is called
- Return value
- the value a procedure sends back to the code that called it
- Modularity
- dividing a program into separate procedures that each handle one task