← Back to course
1/6
Week 06 · Procedures and Abstraction

Monday

The same code, three times
// Splitting a big problem into pieces you can reuse
⏱ about 20 min

Monday: The Same Code, Three Times

Beacon's pseudocode now fills four pages. Comet flips through it, looking for the light check.

"Here is the water average," she says. "And here is the light average. And the temperature average."

Wren lays the three pages side by side. "What do you notice?"

Comet squints. "They are the same eight lines. Only the list name changes."

"And when you fixed a bug in one copy last week," Wren says, "did you fix the other two?"

Comet groans. She did not.

Nova hovers over the pages. "Would you like a hint?" she asks.

"Give those eight lines a name. Then Beacon can call them whenever it needs an average."

This week's driving question

How do programmers split a big problem into pieces they can reuse?

This week you will write procedures for Beacon, plan its modules and look at shared code with care.

What is a procedure?

A procedure is a named group of programming instructions. It may have parameters and return values.

Different languages use different names for procedures, such as method or function.

Once a procedure is written and tested, you can call it by name as many times as you like.

Building on existing correct algorithms saves time. It reduces development time, reduces testing and makes errors easier to find.

PROCEDURE average(numbers)
{
  total ← 0
  count ← 0
  FOR EACH num IN numbers
  {
    total ← total + num
    count ← count + 1
  }
  RETURN(total / count)
}

The name of this procedure is average. Its parameter, numbers, is the list it works on.

RETURN sends the answer back to whatever line called the procedure.

READ THE PROCEDURE
  • Read the question.
  • Tap your answer.
What is the name of this procedure?
What is its parameter?
What does the procedure send back?
Which week's skill does the body use to add up the list?
Before: three copiesAfter: one procedure
Eight lines for the water averagewaterAverage gets average(waterReadings)
Eight lines for the light averagelightAverage gets average(lightReadings)
Eight lines for the temperature averagetempAverage gets average(tempReadings)
A bug must be fixed three timesA bug is fixed once, in one place
StatementTrue or false?
A procedure is a named group of programming instructions.?
Some languages call procedures methods or functions.?
A procedure can be called only once in a program.?
Reusing a tested procedure can reduce testing time.?
WHY THIS EXERCISEKnowing what a procedure is, and why it helps, is the base for everything this week.
Other languages may call a procedure a method or a ____. Type the missing word.
WHY THIS EXERCISEYou will meet all three names, and they describe the same big idea.
TURN REPEATED CODE INTO A PROCEDURE
  • ?Give the repeated code a clear name.
  • ?Find code that repeats with only small differences.
  • ?Replace each copy with a call to the procedure.
  • ?Turn the part that changes into a parameter.
  • ?Test the procedure with a few different arguments.
WHY THIS EXERCISEThese steps turn messy copies into one reusable piece, which is the heart of this week.
On paper, sketch the average procedure as a machine: a list goes in one side, a single number comes out the other.

Strong start, developer. Tomorrow you will call procedures with arguments and trace their return values.