Learning outcomes
By the end of this lesson you should be able to: wrap a set of steps in a function with def; pass values in as parameters and send a value back with return; describe why return and print are different jobs; and use a function as the first real tool for composing a program out of reusable pieces.
Prerequisites
Lessons 1-6. You need variables, conditions and loops, and the debugging idea that a program is a sequence of steps. A function is precisely a named chunk of such steps, so those foundations all apply inside it.
The big idea
Up to now, every program you wrote was one long list. That is fine for a short program, but it does not scale. A function changes the relationship you have with your code. You stop thinking, "what is the next line?" and start thinking, "what are the pieces of this problem?" — and each piece becomes a function. This is the first step toward the decomposing mindset you will build in Module 4, and it is the single tool that makes longer programs possible at all.
Why this matters before the syntax
There are three things a function does, and they are easy to conflate. A function takes inputs — these are the parameters, the placeholders in its definition that get filled in each time you call it. A function does work — the block inside. And a function can hand back a result — the return value. Understanding that a function is a machine with inputs, a body, and an output is more useful than memorising the syntax.
The distinction that trips up beginners most is return versus print. They look similar but are fundamentally different. print writes something to the screen; return hands a value back to whichever part of the program called the function, and that value can be used — stored, passed on, or returned again. A function that prints and a function that returns are not interchangeable. If you call a function in an arithmetic expression and it only prints, you will get an error, because a printing function hands back nothing (it returns None).
Finally, understand the order of events. Python reads the def to learn the recipe, but it does not run the body when it reads the def. The body runs only when the function is called. This is why a function can be defined long before it is used, and why an error inside a function does not surface until you actually call it. Defining a function is writing a recipe; calling it is cooking.
Worked example 1: define and call
The simplest function. It takes nothing in, does one job, and is called twice — so the job happens twice, from a single recipe.
def greet():
print("Hello!")
greet()
greet()Hello!
Hello!The def line names the function and ends with a colon, just like the control statements — the indented block that follows is its body. The function is not run here; it is merely defined. When Python reaches greet() later, it looks up the recipe and runs its body. This is why you see Hello! twice: the recipe was written once, and asking for it twice produced two greetings with no duplicated code.
Worked example 2: parameters
A function that takes two inputs and returns their sum. The values you pass in each call fill the placeholders named in the definition.
def add(a, b):
result = a + b
return result
print(add(3, 4))
print(add(10, 20))7
30When you call add(3, 4), Python stores 3 into the parameter a and 4 into b, runs the body, computes the sum, and returns it. The return sends 7 back out to wherever add(3, 4) was used — here to print. The second call does the same, with different values, giving 30. Notice the function is identical both times; only the inputs changed. That is the power of a parameter: one recipe, many values.
Worked example 3: return vs print
This function returns a built string rather than printing it. The caller decides what to do with the returned value — here, print it.
def describe(name, age):
return name + " is " + str(age)
print(describe("Ada", 19))Ada is 19The function builds a string and returns it; it does not itself print anything. The print is outside, in the calling code. This separation is the point: a well-built function produces a value and lets the caller decide whether to print it, store it, or pass it along. It also shows why str(age) is needed — concatenating text with a number requires conversion, a lesson from earlier that still applies inside functions.
Worked example 4: a default argument
A parameter can be given a default. Call the function with one argument and the default is used; supply two and it is overridden.
def power(base, exp=2):
return base ** exp
print(power(3))
print(power(2, 5))9
32The function can be called two ways because exp has a default of 2. The first call supplies only the base, so exp falls back to 2 and computes 3 squared, which is 9. The second call supplies both arguments, so the default is ignored and 2 to the power 5 is computed, giving 32. Defaults are a convenience, not a substitute for thinking: a default should be the most common value, so callers write less when the usual case applies.
Common mistakes
Watch outConfusing return with print. Paint the difference: print writes to the screen and returns None; return hands a value back to the caller. A function that only prints cannot be used where a value is needed.
Watch outForgetting the def body is not run until the call. Creating a function with def does not execute it. If you define a function and nothing happens, you have not called it.
Watch outDefining a parameter list but not using the returned value. A function that returns nothing gives None, so print(add(3,4)) inside an arithmetic step can produce unexpected None results.
Watch outPlacing side effects (like print) inside a function that later needs to return data. Keep the function's job focused: if it computes a value, return it and let the caller handle output.
Check your understanding
When does the body of a function defined with def actually run?
What is the difference between return and print?
What is the difference between a parameter and an argument?
Multiple choice
Which keyword defines a function in Python?
What does the return statement do?
When is a function's body executed?
In def greet(name), what is name?
What does a function that calls print but has no return produce when used in an assignment?
What is the main benefit of putting code in a function?
Which arithmetic operator does power(base, exp) use to raise base to the exponent?
Why must a small helper function return a value rather than print it when its result feeds another calculation?
Summary and next
A function is a named, reusable block of steps: inputs come in as parameters, the body runs on each call, and return hands a value back to the caller. print and return are different jobs, and the body runs only at the call. Functions let you think in pieces, which is precisely the direction the course is heading.
Next: lists and dictionaries. Functions collect actions into named pieces; the next lesson collects values into named structures — lists for an ordered sequence, dictionaries for a set of key-value pairs — and shows the word-count idiom that is the heart of the PY4E course.