CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 07 / BEGINNER PYTHON

Python functions: arguments and return values

A function is a named piece of reusable behaviour. Parameters name its inputs, and return hands a result back to the caller.

The idea

Imagine a small machine labelled make_label. Give it a name and it gives you a formatted label. Writing that operation once helps you change it in one place. A function definition describes the behaviour; a function call asks Python to perform it. Names created inside the function normally belong to that call, so you can reason about the little job separately.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
def make_label(name):
    return "Evidence for " + name

label = make_label("Christine")
print(label)
Run and change this example ↗
Reveal the expected output
Evidence for Christine

Read it one step at a time

  1. def introduces the function. name is its parameter: a placeholder for the input to a call.
  2. return provides the result and ends this call. It does not display the result by itself.
  3. The call supplies "Christine" as an argument. We store the returned text, then print it.

Make one change

Call make_label a second time with "Dustin" and print the result.

Check your thinking

You should see Evidence for Dustin on a new line, without rewriting the function.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.