# What a Function Is: def and Calling

A function gives a job a name, written once with def and run whenever you call that name with parentheses, so a program does the same work in many places and changes it in one.

Novice · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Define a function with def and call it by its name with parentheses
- Split a program into small functions that each do one job, and describe each one with a docstring

## Key takeaways

- A function is a named job. You write it once with def, then run it as many times as you like by calling its name.
- A function line has def (read "define"), a name, parentheses and a colon, and the indented lines under it are the function's body.
- Calling a function takes its name and parentheses, such as show_header(), and the function must be defined above the line that calls it.
- Give each function one job and a name that says what it does, and describe it in a docstring, a sentence in triple quotes on its first line.

Every program longer than a screen does some job more than once: printing a report's heading, working out a price, checking an answer. A function lets you write that job once, give it a name, and run it whenever you need it. Nearly every Python program you'll read from now on, including the tests that check your exercises, is built from functions.

This chapter's example is Stride, a step-counter app. It counts the steps you walk, turns them into miles (2,000 steps make a mile), and prints a short report after every walk.

## The same lines, again and again

Every Stride report starts with the same three lines. Here are two reports, the morning's and the evening's:

```python
print("Stride")
print("Every step counts")
print("----------")
print("Morning walk: 3200 steps")

print("Stride")
print("Every step counts")
print("----------")
print("Evening walk: 4100 steps")
```

```text
Stride
Every step counts
----------
Morning walk: 3200 steps
Stride
Every step counts
----------
Evening walk: 4100 steps
```

It works, but the heading is copied. Add a lunchtime report and it's copied three times. Then the app changes its slogan from *Every step counts* to *One more step*: you change it in three places, and if you miss one, two reports say one thing and the third says another. Copied code is a promise to remember every copy.

## A recipe card: def and a name

A recipe card is written once and used every time you bake. You don't rewrite the steps for each batch; you say "make the banana bread" and follow the card. A **function** is a recipe card for code: a job with a name.

You write one with `def` (read "define"), the start of a **function definition**:

```python
def show_header():
    print("Stride")
    print("Every step counts")
    print("----------")
```

Run this file and nothing is printed. That's expected: `def` writes the recipe card, and nobody has followed it yet. Each part of the first line has a job:

- `def` says a function definition starts here.
- `show_header` is the function's name, written in snake_case like a variable's name.
- `()`, the parentheses, are where a function's inputs go. This one needs none, so they're empty; the next lesson fills them.
- `:`, the colon, ends the line, as it does after `if`.

The indented lines under it are the function's **body**: the steps on the card. The body ends where the indentation ends, exactly as an `if` block does.

## Calling a function

To follow the card, you **call** the function: its name, then parentheses. `show_header()` is read "call show header". Now the two reports need one line each for their heading:

```python
def show_header():
    print("Stride")
    print("Every step counts")
    print("----------")


show_header()
print("Morning walk: 3200 steps")
show_header()
print("Evening walk: 4100 steps")
```

```text
Stride
Every step counts
----------
Morning walk: 3200 steps
Stride
Every step counts
----------
Evening walk: 4100 steps
```

The output is the same as before, but the slogan lives in one place. Change it inside `show_header` and every report changes with it.

Two things trip people up at first:

- **The parentheses do the calling.** `show_header` on its own names the function without running it, so a line with only `show_header` on it prints nothing. You need `show_header()`.
- **Define first, then call.** Python reads your file from the top. If a call comes before its `def`, Python stops with `NameError: name 'show_header' is not defined`, because it hasn't read the recipe card yet. Put your functions at the top of the file and the calls under them.

The two blank lines between a function and the code after it aren't required. They're the spacing Python programmers agree on, and they make each function stand out.

## One job per function, and a docstring

A good function does one job, and its name says what that job is, usually with a verb: `show_header`, `show_footer`. A bigger job is made from smaller ones, because a function can call another function.

A **docstring** describes the job. It's a sentence in triple quotes, `"""` (read "triple quotes"), on the first line of the body. VS Code shows it when you hover over the function's name anywhere in your code, so whoever calls the function knows what it does without reading its body:

```python
def show_header():
    """Print the three lines at the top of every Stride report."""
    print("Stride")
    print("Every step counts")
    print("----------")


def show_footer():
    """Print the line at the bottom of every Stride report."""
    print("Keep moving. See you tomorrow!")


def show_morning_report():
    """Print the whole morning report: the header, the walk and the footer."""
    show_header()
    print("Morning walk: 3200 steps")
    show_footer()


show_morning_report()
```

```text
Stride
Every step counts
----------
Morning walk: 3200 steps
Keep moving. See you tomorrow!
```

Read the last line and you know the whole program prints the morning report. Each function is short enough to check at a glance, and when the footer changes, you know exactly where to look.

## Do it: print Stride's goal-met badge

When a walker reaches their step goal, Stride prints the same three-line badge to celebrate. Several reports need it, so it belongs in a function.

**Your job:** write a function named `show_badge`, with no inputs.

- Each time it's called, it prints exactly three lines: ten stars, `**********`, then `Goal met!`, then the ten stars again.
- The first line of its body is a docstring that says what it does.

Worked through: calling `show_badge()` once prints `**********`, `Goal met!` and `**********`, one per line. Calling it twice prints those three lines twice, six lines in all.

Practiced live on the lesson page: write the Python right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

Every program you write from here on is a set of small, named jobs that call each other, and `def` is how you make one.

## Key terms

- [Function](https://www.ask-codey.com/glossary/function-python)
- [def](https://www.ask-codey.com/glossary/def)
- [Docstring](https://www.ask-codey.com/glossary/docstring)

## Related

- [Parameters and Return Values](https://www.ask-codey.com/lessons/python-functions-parameters-and-return)
- [Default and Keyword Arguments](https://www.ask-codey.com/lessons/python-functions-default-and-keyword-arguments)

Source: https://www.ask-codey.com/lessons/python-functions-def-and-call
