01 / 20

Developing
Procedures

Learn to write reusable, parameterized procedures that organize code, reduce complexity, and make programs easier to read and maintain.

AP Computer Science Principles

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.

Name
A unique identifier you choose so the procedure can be called by name.
Parameters
Input values (variables) that let the procedure behave differently each time.
Body
The block of instructions that runs every time the procedure is called.

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.

1
Reduce duplication: Write code once and call it many times instead of copying and pasting.
2
Improve readability: Descriptive procedure names make your code self-documenting.
3
Simplify debugging: Fix a bug in one procedure and every call site is automatically fixed.
4
Enable collaboration: Team members can work on different procedures at the same time.

Anatomy of a Procedure

PROCEDURE calculateArea(width, height)
{
\(area \leftarrow width \times height\)
RETURN \(area\)
} Name Parameters Body RETURN sends a value back

Parameters vs Arguments

These two terms are closely related but describe different things. Understanding the distinction helps you communicate precisely about your code.

Parameter
A variable in the procedure definition that acts as a placeholder for future input.
Argument
A specific value passed to the procedure when it is called.
-- Definition: width and height are PARAMETERS PROCEDURE calculateArea(width, height) { RETURN \(width \times height\) } -- Call: 5 and 10 are ARGUMENTS result \(\leftarrow\) calculateArea(5, 10)

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.

Inputs Procedure (implementation hidden) Output The caller only sees inputs and output, not the internal code.

How to Develop a Procedure

1
Identify the task: What specific, repeatable action does this procedure perform?
2
Choose a descriptive name: Use a verb phrase that describes the action (e.g., calculateTotal).
3
Define parameters: Decide what inputs the procedure needs to be flexible.
4
Write the body: Implement the algorithm using the parameters.
5
Decide on return: Should it return a value, or just perform a side effect?
6
Test with different arguments: Call it with various inputs to verify correctness.

Example 1: Simple Procedure

Task: Write a procedure that takes a student's score and returns whether they passed (score > 60).

PROCEDURE hasPassed(score) { IF (\(score > 60\)) { RETURN true } ELSE { RETURN false } }

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.

PROCEDURE countAbove(numList, threshold) { count \(\leftarrow\) 0 FOR EACH item IN numList { IF (\(item > threshold\)) { count \(\leftarrow\) count + 1 } } RETURN count }

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.

PROCEDURE average(numList) { total \(\leftarrow\) sumList(numList) RETURN \(total \div \text{LENGTH}(numList)\) } PROCEDURE sumList(numList) { total \(\leftarrow\) 0 FOR EACH item IN numList { total \(\leftarrow\) total + item } RETURN total }

Tracing a Procedure Call

Caller
\(result \leftarrow \text{average}([10, 20, 30])\)
average([10, 20, 30])
\(total \leftarrow \text{sumList}([10, 20, 30])\)
sumList([10, 20, 30])
RETURN 60
\(total = 60 \rightarrow \text{RETURN } 60 \div 3 = 20\)
RETURN 20
\(result = 20\)

AP Exam Pseudocode Notation

The AP CSP exam uses a specific pseudocode style. Know these patterns:

-- Defining a procedure with return PROCEDURE name(param1, param2) { <instructions> RETURN expression } -- Defining a procedure without return PROCEDURE name(param1, param2) { <instructions> } -- Calling a procedure result \(\leftarrow\) name(arg1, arg2) name(arg1, arg2)

Common Mistakes

Missing RETURN
Forgetting the RETURN statement means the procedure computes a result but never sends it back to the caller.
Wrong Argument Order
Passing arguments in the wrong order causes parameters to receive incorrect values, producing silent bugs.
Too Broad a Procedure
Writing one giant procedure that does everything defeats the purpose. Break tasks into smaller, focused procedures.

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

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

What does addThenDouble(3, 5) return?

Question 5 of 5

What is the value of myTotal after this code runs?