Developing
Procedures
Learn to write reusable, parameterized procedures that organize code, reduce complexity, and make programs easier to read and maintain.
What Is a Procedure?
A procedure (also called a function or method) is a named group of programming instructions that performs a specific task. Instead of writing the same code over and over, you write it once inside a procedure and call it whenever you need it.
Why Procedures Matter
Procedures are one of the most powerful tools in programming. They let you manage complexity by breaking a large problem into smaller, named pieces that you can develop, test, and reuse independently.
Anatomy of a Procedure
Parameters vs Arguments
These two terms are closely related but describe different things. Understanding the distinction helps you communicate precisely about your code.
With vs Without Return Values
| Returns a Value | No Return Value | |
|---|---|---|
| Purpose | Computes and sends back a result | Performs an action (display, move, etc.) |
| Keyword | RETURN value | No RETURN statement |
| Usage | \(result \leftarrow \)proc(args) | proc(args) |
| Example | calculateArea(5, 10) \(\rightarrow\) 50 | displayMessage("Hello") |
Procedural Abstraction
Procedural abstraction means you can use a procedure without knowing how it works internally. You only need to know its name, parameters, and what it returns. This "black box" view is a core AP CSP concept.
How to Develop a Procedure
Example 1: Simple Procedure
Task: Write a procedure that takes a student's score and returns whether they passed (score > 60).
Example 2: Multiple Parameters
Task: Write a procedure that takes a list of numbers and a threshold, then returns the count of numbers above that threshold.
Example 3: Procedures Calling Procedures
A key pattern: one procedure can call another. This builds layers of abstraction, where each layer handles a smaller piece of the problem.
Tracing a Procedure Call
AP Exam Pseudocode Notation
The AP CSP exam uses a specific pseudocode style. Know these patterns:
Common Mistakes
Summary Comparison
| With Parameters | Without Parameters | |
|---|---|---|
| Flexibility | High: works with different inputs | Low: always does the same thing |
| Reusability | Very reusable across contexts | Limited to one specific task |
| Example | greet(name) | greetAlice() |
| AP Exam Focus | Heavily tested | Less common |
| With Return | Without Return | |
|---|---|---|
| Use case | Computing a value | Performing an action |
| Can assign result? | Yes: \(x \leftarrow \text{proc()}\) | No: just call proc() |
Question 1 of 5
What is the primary benefit of using procedural abstraction?
Question 2 of 5
Consider the following procedure:
PROCEDURE mystery(a, b)
{
IF (\(a > b\))
{
RETURN a
}
ELSE
{
RETURN b
}
}
What does mystery(7, 12) return?
Question 3 of 5
In the AP pseudocode, what is the difference between a parameter and an argument?
Question 4 of 5
Consider the following two procedures:
PROCEDURE double(x)
{
RETURN \(x \times 2\)
}
PROCEDURE addThenDouble(a, b)
{
sum \(\leftarrow\) a + b
RETURN double(sum)
}
What does addThenDouble(3, 5) return?
Question 5 of 5
A student writes the following procedure:
PROCEDURE calculateTip(billAmount, tipPercent)
{
tip \(\leftarrow\) billAmount \(\times\) (tipPercent / 100)
total \(\leftarrow\) billAmount + tip
}
The student then writes: \(myTotal \leftarrow \text{calculateTip}(50, 20)\)
What is the value of myTotal after this code runs?