AP CSP Day 28: Procedures & Parameters

Key Concepts

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.

Common Trap: Thinking a procedure that modifies a local variable affects the original argument. Local variables are copies. Only returned values or side effects on shared structures persist outside the procedure.
Exam Tip: When tracing procedure calls, mentally box off the procedure code. Inside the box, only the parameters and local variables exist. The return value is what escapes the box.
Big Idea 3: Algorithms & Programming
Cycle 1 • Day 28 Practice • Medium Difficulty
Focus: Procedures & Parameters

Practice Question

What is displayed after the following code runs?

PROCEDURE mystery(a, b)
{
   RETURN a + b * 2
}
DISPLAY(mystery(3, 4))
Why This Answer?

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.

Why Not the Others?

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.

Common Mistake
Watch Out!

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.

AP Exam Tip

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
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.