Advanced

Splitting a program into functions

A program that reads like a list of steps is a program made of functions. Each function is one step, and the main code names the steps in order.

What you will see

4

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Split one block into three functions.

1

Write three one-step functions.

def read_data(): return [1, 2, 3] def work(items): return sum(items) def show(total): print(total)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
no output yet
2

Run them in order.

show(work(read_data()))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
6
3

Put the plan in a main function and call it.

def main(): items = read_data() total = work(items) show(total) main()

Before you run it, say out loud what it will print. Then open the answer.

What it prints
6
4

Call one piece alone with your own values.

print(work([2, 2]))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Each idea becomes its own function, and the program is the list of those names in order.
1 read_data returns items
↓
2 work returns a total
↓
3 show prints it
↓
4 main names the steps
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def read_data(): sets nothing on screen
return [1, 2, 3] sets nothing on screen
def work(items): sets nothing on screen
return sum(items) sets nothing on screen
def show(total): sets nothing on screen
print(total) no output yet
Why this works
Top down

Start by naming the steps of the whole job. Each name becomes a function, and each function is split the same way until the steps are one or two lines.

Why small functions help

A small function has a name, an input, and an output. That shape can be read, tested, moved, and deleted on its own. A long block cannot.

Examples

Name the steps

def read_data():
    return [1, 2, 3]

def work(items):
    return sum(items)

def show(total):
    print(total)

show(work(read_data()))

Three functions, three steps. The last line reads as do this, then this, then this.

One function per idea

def celsius(f):
    return (f - 32) * 5 / 9

def report(t):
    return str(round(t)) + "C"

print(report(celsius(68)))

Converting and formatting are different ideas. Keeping them apart means each one can be checked on its own.

The main function

def main():
    items = read_data()
    total = work(items)
    show(total)

main()

main holds only the plan. Every detail lives one level down, which is what makes the plan readable at a glance.

Test one piece

def work(items):
    return sum(items)

print(work([2, 2]))

Because work takes its input and returns its output, you can call it alone with values you chose. Testing a piece needs no setup.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Returning more than one thing
Previous Lesson
Returning more than one thing
Next Lesson
Not repeating yourself
Not repeating yourself →