Basic

What a function is

A function is a named block of steps you can run again and again. You write it once and call it by name as many times as you like.

What you will see

NameError

About 2 min of reading

Try It

Make and call your first function.

1

Define a function that prints a line.

def greet(): print("hi")

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

What it prints
no output yet
The def only makes the function. Nothing prints until you call it.
2

Call it once.

greet()

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

What it prints
hi
3

Call it again to see reuse.

greet()

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

What it prints
hi
4

Put the call above the def and watch it fail.

call_it() def call_it(): print("late")

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

What it prints
NameError
A name must exist before it is used. Definitions go above the code that calls them.

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

The def line creates a named block, and a call by that name runs the steps inside it.
1 def makes the name
↓
2 the body holds steps
↓
3 a call runs the body
↓
4 the name can be reused
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 greet(): sets nothing on screen
print("hi") no output yet
Why this works
Why wrap steps in a name

A name gives a reader a word for a job. Instead of reading the same three lines every time they appear, you read the name and know the job. That is the first reason functions exist.

When you have a choice

Definition versus call

A definition describes a job and runs nothing. A call runs the job. Keeping these two apart is the mental model every later lesson builds on.

Examples

Write it, then run it

def greet():
    print("hello")

greet()

The def line makes the function but runs nothing. The call greet() on its own line is what runs the steps inside.

A name for a job

def show_total():
    print(2 + 3)

show_total()
show_total()

The name show_total stands for the job. Calling it twice runs the same two steps twice, which is the whole point of a function.

Empty body is not allowed

def placeholder():
    pass

placeholder()

A function body cannot be blank, so pass is the placeholder that means do nothing. It keeps the shape valid while you fill in the steps later.

Order matters

run_it()

def run_it():
    print("ok")

Calling a function before it is made fails. The def has to run first so the name exists, which is why definitions go above the calls.

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.

Next Lesson
Running a function with a result
Running a function with a result →