AP CSP Topic 3.13 Guided Notes - Developing Procedures

Big Idea 3: Algorithms and Programming · Topic 3.13 · Guided Notes (Student)

Developing Procedures — Guided Notes

Fill these in during class or catch up here if you were absent. Print this page or work on paper — then check yourself with the CFUs on the Topic 3.13 page.

Print these notesAll CSP topics

Day 1: One Name, Many Uses

Today’s objectives

  • Explain how procedural abstraction manages complexity by naming a process, dividing a program into modules, and reusing shared code instead of duplicating it (LO AAP-3.B)
  • Explain how parameters generalize a procedure so it works for many inputs, how procedures aid readability, and why the internals can change without notifying callers (LO AAP-3.B)

Bell ringer

The school store prints a price label for every product. The program below computes each label's total the SAME way, copied three times: pencilTotal ← 3 * 12, then eraserTotal ← 2 * 30, then notebookTotal ← 5 * 8. Tomorrow the store adds a 6th, 7th, and 8th product — and the manager wants to switch every total to charge by the dozen.

Write 2–4 sentences: What is painful about this design as the store grows? If the pricing rule changes, how many places must you edit, and what could go wrong? Sketch a better design where the 'multiply unit price by quantity' idea lives in exactly ONE place.

01. Why Procedures: Naming, Modularity, Reuse

Key Vocabulary (LO AAP-3.B)

Term Definition (write it)
Procedure
Procedural abstraction
Modularity
Code reuse
Parameter

Use It Knowing WHAT, Not HOW

  • Procedural abstraction lets a procedure be used knowing only .
  • It lets the solution to a large problem be built from .
  • You already rely on this when you call DISPLAY or RANDOM because .

The Store's Pricing Rule, Named Once

labelTotal wraps 'unit price times quantity' in one name; the program just calls it.

AP Pseudocode (what the exam tests)

PROCEDURE labelTotal(unitPrice, quantity)
{
    RETURN(unitPrice * quantity)
}
DISPLAY(labelTotal(3, 12))

Python (the runnable version)

def labelTotal(unitPrice, quantity):
    return unitPrice * quantity

print(labelTotal(3, 12))

Output

36

Run and edit this yourself in the coding exercises for this topic.

Copy the Formula vs. Call One Procedure

Duplicated code

One procedure, reused

  • The design on the right survives a rule change because the shared logic lives .
  • With the duplicated design, changing the pricing rule forces you to .
  • Two designs can print identical output today yet behave very differently when .

AP TIP: Modularity = splitting the program into subprograms; reuse = one procedure instead of duplicated code. Both manage complexity.

Stop and think

  1. In two sentences, explain what it means to use a procedure 'knowing only what it does, not how it does it.' Use DISPLAY as your example.
  2. The store copies its pricing formula into 8 products. Explain, using the words modularity and reuse, why moving the formula into one labelTotal procedure manages complexity.
  3. A classmate says 'duplicating the formula and calling one procedure are the same — both print the right totals.' They print the same now; explain the situation in which the two designs behave very differently.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.13 page.

02. Parameters, Readability, Hidden Internals

Parameters Generalize; Internals Stay Hidden

  • Adding parameters generalizes a procedure so it can .
  • Beyond correctness, naming the process improves .
  • You may rewrite a procedure's internals without notifying callers as long as .

One Definition, Many Products

The same labelTotal serves every product; only the arguments change.

AP Pseudocode (what the exam tests)

DISPLAY(labelTotal(3, 12))
DISPLAY(labelTotal(2, 30))
DISPLAY(labelTotal(5, 8))

Python (the runnable version)

print(labelTotal(3, 12))
print(labelTotal(2, 30))
print(labelTotal(5, 8))

Output

36 60 40

Run and edit this yourself in the coding exercises for this topic.

What Each Design Move Buys You

Each row is a move you make when you build a procedure. Fill in what complexity it manages, then name the CED idea. The middle column is yours to complete.

Complete the empty cells.

Move in the code What it buys you (fill in) CED idea
Wrap the pricing formula in a named procedure You use it by name, knowing what it does, not how Procedural abstraction (B.1)
Split the program into small procedures Modularity (B.3)
Call one procedure instead of copying the formula Code reuse (B.4)
Add a parameter like quantity One procedure serves a whole range of inputs Generalization (B.5)
Rewrite the formula inside to round differently Hidden internals (B.7)

Watch out — “Change the Inside, Update Every Call”

Myth: If you change what is inside a procedure, you must go update every place in the program that calls it.

Explain why this is wrong:

Stop and think

  1. Explain in one or two sentences how the parameters of labelTotal(unitPrice, quantity) let a single procedure price a pencil, an eraser, and any future product.
  2. A teammate speeds up a procedure's internals but keeps its result identical. Explain why nobody who calls it has to change their code, using the phrase 'what it does is preserved.'
  3. Give one concrete way that replacing duplicated code with a well-named procedure improves the READABILITY of a program.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.13 page.

Today in one box

  • Procedural abstraction names a process so you use it knowing WHAT it does, not how
  • It builds a large solution from smaller subproblems, and modularity is that subdivision into subprograms
  • Reuse means one procedure instead of duplicated code; parameters generalize it to a range of inputs
  • Procedures improve readability, and hidden internals can change without notifying callers if the behavior is preserved

Before next class: Before tomorrow, predict it: a procedure runs IF(quantity = 0){ RETURN(0) } and then RETURN(price * quantity). What does it return for charge(5, 0), and does the second RETURN ever run? Write your answer and your reasoning.

Day 2: Writing the Procedure

Today’s objectives

  • Write a PROCEDURE with parameters and no RETURN, using the reference-sheet form PROCEDURE procName(p1, p2) { block } (LO AAP-3.C)
  • Write a PROCEDURE that returns a value with RETURN(expression), knowing RETURN may appear anywhere and causes an immediate return to the caller (LO AAP-3.C)

Bell ringer

Yesterday's teaser, now decide it. A procedure runs: IF(quantity = 0) { RETURN(0) } and then, below that, RETURN(price * quantity). Consider two calls: charge(5, 0) and charge(5, 3).

Write down: What does charge(5, 0) return, and does the RETURN(price * quantity) line ever run for that call? What does charge(5, 3) return? In one sentence, state the rule about when a RETURN stops the rest of the procedure.

01. Writing a PROCEDURE (No Return)

PROCEDURE name(params) { block }

  • The first line of a procedure — keyword, name, and parameter list — is called the .
  • The block of statements inside the braces is called the .
  • The reference-sheet form PROCEDURE procName(...) { block } defines a procedure that takes .

A Procedure That Prints a Label

printLabel takes two parameters and DISPLAYs them; it returns no value.

AP Pseudocode (what the exam tests)

PROCEDURE printLabel(item, price)
{
    DISPLAY(item)
    DISPLAY(price)
}
printLabel("Pencil", 3)

Python (the runnable version)

def printLabel(item, price):
    print(item, end=" ")
    print(price)

printLabel("Pencil", 3)

Output

Pencil 3

Run and edit this yourself in the coding exercises for this topic.

Parameters (in the Header) vs. Arguments (at the Call)

Parameters

Arguments

  • A parameter is a placeholder in the header that stays empty until .
  • The first supplied value fills the first parameter and the second fills the second — the matching is done by .
  • Supplying different values on each call is exactly what lets one procedure .

AP TIP: Parameters are named ONCE in the header; arguments are supplied EACH time you call. Position matters — first argument fills the first parameter.

Stop and think

  1. Write, in exam pseudocode, a PROCEDURE greet(name) with no return that DISPLAYs the name. Label the header and the body.
  2. In greet("Ada"), identify the parameter and the argument, and state which one is named in the header and which is supplied at the call.
  3. A classmate writes PROCEDURE printLabel { DISPLAY(item) } with no parentheses and no parameters, then calls printLabel("Pencil", 3). Explain what is wrong with the header.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.13 page.

02. Writing a PROCEDURE with RETURN

PROCEDURE name(params) { block RETURN(expression) }

  • RETURN(expression) evaluates the expression and then .
  • Within a procedure, a RETURN statement may appear .
  • The instant a RETURN runs, it causes .

A Procedure That Returns a Total

finalPrice computes the total and RETURNs it; the caller stores the value.

AP Pseudocode (what the exam tests)

PROCEDURE finalPrice(price, quantity)
{
    total ← price * quantity
    RETURN(total)
}
receipt ← finalPrice(3, 12)
DISPLAY(receipt)

Python (the runnable version)

def finalPrice(price, quantity):
    total = price * quantity
    return total

receipt = finalPrice(3, 12)
print(receipt)

Output

36

Run and edit this yourself in the coding exercises for this topic.

Trace an Early RETURN, Call by Call

The procedure is: PROCEDURE charge(price, quantity) { IF(quantity = 0) { RETURN(0) } RETURN(price * quantity) }. For each call, name which RETURN fires and the value returned. The last column is yours to complete.

Complete the empty cells.

Call Which RETURN fires Value returned (fill in)
charge(5, 0) the first RETURN — quantity = 0, immediate exit 0
charge(5, 3) the second RETURN — guard skipped
charge(4, 1) the second RETURN — guard skipped
charge(10, 0) the first RETURN — quantity = 0, immediate exit

Watch out — “DISPLAY and RETURN Do the Same Thing”

Myth: A procedure that DISPLAYs its result has effectively given that value back to whoever called it.

Explain why this is wrong:

Stop and think

  1. Write, in exam pseudocode, a PROCEDURE doublePrice(price) that RETURNs price times 2. Then write one line that stores doublePrice(9) in a variable and DISPLAYs it.
  2. Rewrite charge so it RETURNs 0 when quantity is 0. Explain why the RETURN(price * quantity) line is never reached when quantity is 0.
  3. A procedure ends with DISPLAY(total) instead of RETURN(total). Explain what breaks when a caller writes cost ← thatProcedure(5, 4) and why.

Show your procedures. Then check yourself with the matching CFUs on the Topic 3.13 page.

Common AP Traps

Three ways Topic 3.13 loses points on the exam — one minute now, real points in May.

RETURN is not DISPLAY — in your own words:

RETURN exits immediately — in your own words:

Change the HOW, keep the callers — in your own words:

Developing Procedures, in One Slide

  • Procedural abstraction manages complexity: it names a process, enables modularity and reuse, generalizes with parameters, aids readability, and hides internals.
  • The no-return form: PROCEDURE procName(parameter1, parameter2...) { block } — a header naming parameters, a body of statements, no value returned.
  • The return form: PROCEDURE procName(...) { block RETURN(expression) } — RETURN evaluates the expression and hands its value back to the caller.
  • RETURN may appear anywhere and causes an IMMEDIATE return; DISPLAY only prints, RETURN hands a value back to be stored or reused.

Exit check — I can…

  • ☐  explain how procedural abstraction manages complexity through naming, modularity, reuse, and hidden internals (LO AAP-3.B)
  • ☐  explain how parameters generalize a procedure so it works for a range of inputs (LO AAP-3.B)
  • ☐  write a PROCEDURE with parameters and no RETURN using the reference-sheet form (LO AAP-3.C)
  • ☐  write a PROCEDURE that RETURNs a value, and place a RETURN so it exits at the right moment (LO AAP-3.C)

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]