Show contents
Chapter 1 of 7 · 20 min
Defining and calling a function
By the end of this chapter, you'll be able to
- Define a function with def
- Pass values to a function through its parameters
- Send back a result with return
- Tell the difference between displaying (print) and returning (return)
Defining and calling
You already use ready-made functions: print(), len(), round(). You can create your own with def (for "define"):
def price_with_tax(net_price, rate):
"""Returns the price including tax."""
return round(net_price * (1 + rate), 2)
print(price_with_tax(100, 0.2))
print(price_with_tax(19.99, 0.055))
120.0
21.09
The exact vocabulary:
- defining a function (
def ...:) creates the recipe, without running it; - the parameters (
net_price,rate) are the variables that receive the values; - calling the function (
price_with_tax(100, 0.2)) runs it with arguments: here 100 and 0.2; returnsends a value back to where the call was made and ends the function;- the string between triple quotes is a docstring: it documents the function.
We display the result.
Text description of the animation
- The def only defines the function. The program really starts on line 4.
- Python reads the def and remembers the function price_with_tax, without running its body.
- Line 4: to compute total, Python first has to call the function.
- Python enters the function: the arguments 100 and 0.2 are stored in the parameters.
- The calculation gives 120.0: return sends this value back and the function ends.
- Back on line 4: the returned value is stored in total. The parameters have disappeared.
- We display the result.
Notice step 6: once the function has finished, its parameters no longer exist. Each call has its own memory.
Displaying is not returning
print() shows a value on the screen, but the program can no longer use it. return hands the value to the code that called the function, which can store it, compare it, reuse it:
def double(x):
return x * 2
result = double(double(5)) # calls can be chained
print(result)
20
Your turn
Mini-lab: currency conversionPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: the largest of three
Write a function maximum(a, b, c) that returns the largest of the three numbers, without using Python's max() function.
Hint 1
a in a variable largest, then compare it with b and with c.Hint 2
ifs in a row are enough: each one replaces largest if it finds something bigger.Show the solution and common mistakes
def maximum(a, b, c):
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
return largest
print(maximum(3, 9, 4), maximum(-1, -5, -2), maximum(7, 7, 2))
9 -1 7
Common mistakes:
- Writing
if/elif: formaximum(1, 5, 9), theelifwould never be tested after the first trueif, and the function would return 5. - Displaying the result with
printinstead of returning it: the function could no longer be reused in a calculation.
Summary
- 1def defines a function; calling it runs it with arguments.
- 2The parameters receive the arguments, in the same order.
- 3return sends back a value and ends the function.
- 4Without return, a function returns None.
- 5print displays, return passes on: a useful function usually returns its value.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.