Basic

A function that builds text

Functions are how text gets built on demand. A function takes the parts it needs and returns the sentence made from them.

What you will see

2. stop

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

Build text with a function.

1

Make a function that returns a scored line.

def report(name, score): return name + " scored " + str(score) print(report("Ada", 9))

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

What it prints
Ada scored 9
2

Call it with different parts.

print(report("Bo", 4))

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

What it prints
Bo scored 4
3

Join a list of words with spaces.

def join_words(words): return " ".join(words) print(join_words(["owl", "ww"]))

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

What it prints
owl ww
4

Number a label from two arguments.

def label(step, text): return str(step) + ". " + text print(label(2, "stop"))

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

What it prints
2. stop

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

A function takes the parts it needs as arguments and returns the text built from them.
1 arguments are the parts
↓
2 the body joins them
↓
3 str makes numbers text
↓
4 the call returns the line
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 report(name, score): sets nothing on screen
return name + " scored " + str(score) sets nothing on screen
print(report("Ada", 9)) Ada scored 9
Why this works
Arguments as blanks

Think of a function as a sentence with blanks. The parameters are the blanks and the arguments are what fills them on a given call.

Build once, use many times

Any text with a fixed shape should be one function. Changing the shape is then one edit, and every use follows.

Examples

Fill in a sentence

def report(name, score):
    return name + " scored " + str(score)

print(report("Ada", 9))

The parts are arguments, and the returned line is built from them. str turns the number into text so the join works.

Reuse with new parts

def report(name, score):
    return name + " scored " + str(score)

print(report("Bo", 4))
print(report("Ada", 9))

One function, many sentences. Only the arguments change, which is the payoff of writing it once.

Build from a list

def join_words(words):
    return " ".join(words)

print(join_words(["owl", "ww"]))

join puts a space between each item. The function works for any list of words you hand it.

A label maker

def label(step, text):
    return str(step) + ". " + text

print(label(1, "start"))
print(label(2, "stop"))

The function fixes the shape and the arguments fill the blanks. Numbered labels are the same job twice.

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.

← Putting calls inside calls
Previous Lesson
Putting calls inside calls
Next Lesson
A function that asks for input
A function that asks for input →