AP CSP Day 58: Procedures & Parameters | Cycle 2

Key Concepts

Higher-order procedure design involves writing a general procedure that accepts another procedure or a condition as a parameter, enabling flexible reuse across different scenarios. On the AP CSP exam, this concept appears when a procedure is designed to apply different operations to a list depending on a parameter value. Cycle 2 procedure review questions ask students to complete or correct a general-purpose procedure so it works correctly for multiple described use cases. Recognizing which part of the algorithm should be fixed (the procedure body, the parameter list, or the calling code) is the key diagnostic skill.

📚 Study the Concept First (Optional) Click to expand ▼

General-Purpose Procedures: Design and Debugging

What Makes a Procedure General-Purpose?

A general-purpose procedure performs a task that is useful across multiple different contexts by parameterizing the parts that vary. Instead of writing separate procedures for each case, one well-designed procedure with the right parameters handles all cases.

Debugging Multi-Use Procedures

When a procedure works correctly in some call contexts but not others, the bug is usually in handling a specific combination of parameter values. Test the procedure with the failing call specifically, trace its execution with those arguments, and compare to the expected result.

Common Trap: Fixing the procedure for one specific call without checking whether the fix breaks other valid calls. A general-purpose procedure must work correctly for all specified use cases simultaneously.
Exam Tip: On procedure design questions, identify what varies across the described use cases and verify that each varying element is controlled by a parameter. Any hardcoded value that should vary is a design flaw.
Big Idea 3: Algorithms & Programming
Cycle 2 • Day 58 Practice • Hard Difficulty
Focus: Procedures & Parameters

Practice Question

What is displayed after the following code runs?

PROCEDURE process(list, threshold)
{
   count ← 0
   FOR EACH item IN list
   {
      IF item ≥ threshold
      {
         count ← count + 1
      }
   }
   RETURN count
}

data ← [15, 22, 8, 30, 12, 25]
high ← process(data, 20)
low ← process(data, 10)
DISPLAY(high)
DISPLAY(low)
Why This Answer?

First call process(data, 20): elements ≥20 are 22, 30, 25 → count=3. Second call process(data, 10): elements ≥10 are 15, 22, 30, 12, 25 → count=5 (only 8 is excluded). high=3, low=5.

Why Not the Others?

B) Five elements are ≥10, not four (12 ≥ 10 is true). C) Three elements are ≥20, not two. D) Not all six elements are ≥10 because 8 < 10.

Common Mistake
Watch Out!

Students often miss boundary values. For threshold=10, the element 12 satisfies 12 ≥ 10. For threshold=20, the element 22 satisfies 22 ≥ 20. Carefully check each element against the threshold.

AP Exam Tip

When a procedure is called multiple times with different arguments, trace each call independently. Do not carry state from one call to the next — local variables reset with each call.

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.