Unit 3 · Programming 2 · Lesson 3.12

Writing Procedures

Copy-paste is a warning sign. When the same lines show up three times, it’s time to write your own procedure — name a job once, then call it wherever you need it.

Big Idea 3 — Algorithms & ProgrammingManaging complexity

Before this lesson — read on Runestone

Focus on defining your own procedure and giving it parameters. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs. In 3.11 you called procedures other people wrote. Now you write your own — the exact skill the Create PT requires, and the tool that turns a sprawling program into a few readable lines.

2

The Hook

Your gradebook has the same 4 lines three times. Once for the quiz scores, once for the tests, once for the final. Copy, paste, tweak. It works — until you find a bug and have to fix it in three places (and miss one).

Repeated code is a code smell — a sign something wants to be a procedure. Write the four lines once as class_average(scores), and now there’s one place to call, one place to fix, one name that says what it does. That is what “managing complexity” means in practice.

3

The Idea

You define a procedure with def (Python) or PROCEDURE (AP). The names in the definition’s parentheses are parameters — placeholders. The values you pass when you call it are arguments — the real values that fill those placeholders.

Parameters vs arguments (a favorite exam distinction): the parameter is the name in the definition (def greet(name): — name is a parameter). The argument is the value at the call (greet("Sam") — "Sam" is the argument). Definition = parameter; call = argument.

Python — define, then call

def triple(n):     # n is a parameter
    return n * 3

answer = triple(5)  # 5 is an argument
print(answer)      # 15

AP Pseudocode

PROCEDURE triple(n)
{ RETURN (n * 3) }

answer ← triple(5)
DISPLAY(answer)

Hover or tap a line to light its twin. The definition (lines 1–2) just describes the job — nothing runs yet. The code runs when you call it (line 4), with 5 flowing into the parameter n. Result: 15.

Procedures are “managing complexity” — and the CED says so. The exam justifies procedures with two words: procedural abstraction and modularity. Both mean the same everyday thing — break a big problem into named pieces you can build, name, and reuse independently. A good procedure name lets a reader understand your program without reading its insides.
4

Try It — Define Once, Call Many

Here’s a defined procedure triple(n). Call it with different arguments and watch the same definition produce different results — that’s the whole point of a parameter.

def triple(n):
    return n * 3
triple( )
parameter n receives: —
returns: —
Change the argument and call it again — same definition, new result.
5

Vocabulary

parameter Programs
A placeholder name in a procedure’s definition that receives an argument when the procedure is called.
procedural abstraction Programs
Packaging a task into a named procedure so it can be used without re-reading its details.
modularity Programs
Building a program from separate, named pieces — each testable and reusable on its own.
6

Check

Five questions. Watch the parameter/argument distinction and remember: defining a procedure doesn’t run it — calling it does. Then pick your answer.

0 of 5 answered
7

Impact Check

Every big program is small programs with names

No one writes a hundred-thousand-line program as one giant script. They write hundreds of small procedures — send_email, check_password, load_level — each named for its job, each testable alone, each reusable. The names are the design: a good one lets a teammate use your code without reading it.

This is how teams build things no individual could hold in their head, and how you’ll build your Create PT: a couple of well-named procedures beat one tangled block every time. “Don’t repeat yourself” isn’t tidiness — it’s the difference between fixing a bug once and hunting it in five places.

8

Connections

Came from 3.11 — you called built-in procedures; now you define your own with the same in-and-out shape.
Next in 3.13 — the make-or-break distinction: does your procedure return a value or only print one?
Returns in 3.14 — a procedure that takes a list parameter is the exact Create PT shape.
Returns on the exam — “which is the parameter / the argument?” and “what does this procedure return?” are guaranteed points.
← 3.11 Calling Procedures