AP CSP Day 28: Procedures & Parameters
Share
When a procedure is called, the argument values are copied into the corresponding parameters, which are local variables visible only inside the procedure. Changes to parameters inside a procedure do not affect variables in the calling code. AP CSP review questions on procedures often show code with multiple procedure calls using different arguments and ask students to trace the return value for each call. Understanding scope, the region of code where a variable is accessible, is essential for correctly predicting procedure behavior.
📚 Study the Concept First (Optional) Click to expand ▼
Procedures Review: Scope and Return Values
Variable Scope
A variable declared inside a procedure is local to that procedure. It does not exist outside the procedure and cannot be accessed by the calling code. Each call to the procedure creates fresh local variables with no memory of previous calls.
Return Values
A RETURN statement ends procedure execution immediately and sends a value back to the calling code. The calling code can store this value, use it in an expression, or ignore it entirely.
Practice Question
What is displayed after the following code runs?
PROCEDURE mystery(a, b)
{
RETURN a + b * 2
}
DISPLAY(mystery(3, 4))Standard order of operations applies: multiplication before addition. Inside the procedure with a=3 and b=4: b * 2 = 4 * 2 = 8, then a + 8 = 3 + 8 = 11.
A) 14 would result from (3+4)*2, which incorrectly adds before multiplying. C) 10 does not correspond to any valid evaluation. B) 7 would be a + b without the multiplication.
Students add a + b first (getting 7) and then multiply by 2 (getting 14), ignoring the standard mathematical order of operations where multiplication precedes addition.
Standard math order of operations applies in AP CSP pseudocode: multiplication and division before addition and subtraction. Use parentheses mentally to clarify ambiguous expressions.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help