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
def make_label(name):
return "Evidence for " + name
label = make_label("Christine")
print(label)Reveal the expected output
Evidence for Christine
Read it one step at a time
- def introduces the function. name is its parameter: a placeholder for the input to a call.
- return provides the result and ends this call. It does not display the result by itself.
- 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.