Topic 3.12 Notes – Calling Procedures
What a Procedure Is
A procedure is a named group of programming instructions that performs a specific task. Other languages might call it a function or method, but on the AP exam, you’ll see procedure.
Why we use them:
- Break big problems into smaller pieces
- Reuse code instead of rewriting it
- Make programs easier to test and manage
In AP pseudocode (from the reference sheet), a procedure looks like this:
PROCEDURE calculateTotal(price, tax)
{
total ← price + tax
RETURN(total)
}
Key parts:
calculateTotal→ procedure nameprice, tax→ parameters (inputs)- Body inside
{ }→ runs when called RETURN(total)→ sends a value back
Procedures can:
- Have parameters or none
- Return a value or not
Understanding that flexibility matters when you trace code.
Parameters, Arguments, and Return Values
Parameters vs Arguments
This distinction shows up in tricky multiple-choice questions.
- Parameter = variable listed in the procedure definition
- Argument = actual value passed in during the call
If we call:
result ← calculateTotal(20, 5)
20and5are argumentspricebecomes 20taxbecomes 5
Arguments are assigned in order:
- First argument → first parameter
- Second argument → second parameter
If the numbers don’t match, the call is invalid.
Procedures That Return a Value
A procedure uses:
RETURN(expression)
Two things happen immediately:
- The value of the expression is sent back.
- The procedure stops running.
Example:
PROCEDURE triple(n)
{
RETURN(n * 3)
}
Call:
x ← triple(4)
Now x stores 12.
From the reference sheet, this format is important:
result ← procName(arg1, arg2)
That means the returned value is assigned to result.
If you see code after RETURN, ignore it. It never runs. The AP loves testing that detail.
Procedures That Do Not Return a Value
Some procedures just do something instead of returning a value.
Example:
PROCEDURE announce(message)
{
DISPLAY(message)
}
Call:
announce("Hello")
This prints output, but you would not assign it to a variable.
If you tried:
x ← announce("Hi")
x would not get a meaningful value because nothing is returned.
Built-In Procedures You Must Know
From the reference sheet:
DISPLAY(expression)- Outputs the value followed by a space.
INPUT()- Accepts user input
- Returns the value entered
- Usually written as:
response ← INPUT()
Remember that INPUT() returns a value. DISPLAY() does not.
How Procedure Calls Affect Program Flow
Programs normally run top to bottom.
When a procedure is called, execution temporarily jumps elsewhere.
Here’s the flow visually. The left side shows the main program, and the right side shows the separate Cook procedure that gets called:
Main routine calling a separate procedure
Notice how control moves from the main flow into Cook, runs through its steps and decision, then returns to the main program.
What actually happens step by step:
- The program reaches a procedure call.
- It pauses the current line.
- Arguments are assigned to parameters.
- The procedure body executes.
- When the last statement runs, or a
RETURNexecutes, control goes back. - Execution resumes immediately after the call.
Students often think it restarts the program or skips ahead. It doesn’t. It returns to the next line.
Determining the Result of a Procedure Call
This is where most test questions live.
When tracing code:
- Plug argument values into parameters.
- Run the procedure logic.
- Determine the returned value (if any).
- Replace the call with that value.
- Continue evaluating the original expression.
Example pattern:
a ← 2
b ← triple(a) + 5
- Evaluate
triple(a)first. - Replace with returned value.
- Then add 5.
Nested calls evaluate inside first.
If a procedure only uses DISPLAY, your job is to determine what prints, not what gets stored.
Why This Matters
Procedures let programmers reuse tested code and generalize solutions using parameters. On assessments, you’ll either:
- Write a correct procedure call, or
- Trace one and determine its output or returned value
Most mistakes happen when students lose track of parameter substitution or forget that RETURN ends execution immediately.