A Python function is a named block of code that does one job. You define it with the def keyword, give it input in parentheses, and send a result back with return. If you need the same calculation in many places, write it once as a function and call it as often as you like.
The examples use Corner Café in the fictional port city Port Alder, where Ada serves drinks and sets the prices.
How do you define a function in Python?
Write def, a name, parentheses, a colon, and an indented body.
def greet(): print("Welcome to Corner Café!") greet() greet()
def greet():defines the function. This line prints nothing by itself.- The indented lines are the function body.
greet()calls the function, and the body runs. Two calls print the message twice.
What is the difference between a parameter and an argument?
A parameter is the name in the definition. An argument is the real value you pass in a call.
def greet(name): print(f"Welcome, {name}!") greet("Maya") greet("Marco")
Here name is the parameter. "Maya" and "Marco" are arguments. The body stays the same, and only the input changes.
What does return do in a Python function?
return sends the result back to the code that called the function. You can store it in a variable or use it in another calculation.
def add_tax(price): return round(price * 1.10, 2) latte = add_tax(4.25) tea = add_tax(1.25) print(latte + tea)
At Corner Café the tax is 10%. The latte costs $4.68 with tax and the tea $1.38, so the code prints 6.06. As soon as return runs, the function ends. Any line below it is skipped.
What is the difference between return and print in Python?
print shows a value on the screen. return gives the value back to your code. Beginners mix them up all the time.
def add_print(a, b): print(a + b) def add_return(a, b): return a + b x = add_print(2, 3) y = add_return(2, 3) print(x, y)
The first 5 comes from the print inside add_print. That function shows 5 but returns nothing, so x is None. The second function returns 5, so y is 5. If you want to use the result later, you need return.
How do default parameters work in Python?
A default value is used when the caller does not pass that argument.
def bill(amount, discount=0, tip=0): return amount * (1 - discount) + tip print(bill(20)) print(bill(20, 0.10)) print(bill(20, tip=2))
discount=0is a default. If you skip it, Python uses 0.tip=2is a keyword argument. It lets you skip a parameter and name the one you mean.
Parameters with defaults go after parameters without defaults.
Can a function return more than one value?
Yes. Separate the values with commas and Python returns them together as a tuple.
def bill_summary(amounts): total = sum(amounts) average = total / len(amounts) return total, average total, average = bill_summary([4.25, 1.25, 2.00]) print(total, average)
The two names on the left of the equals sign unpack the tuple in order.
Why can't you see a variable from inside a function?
Variables created inside a function exist only while it runs.
def add_tax(price): with_tax = price * 1.10 return round(with_tax, 2) result = add_tax(4.25) print(result)
If you wrote print(with_tax) outside the function, you would get a NameError. A function talks to the rest of your program through two doors: inputs come in as parameters, and the result goes out with return.
When should you write a function?
- You are about to copy the same code a second time.
- You can give the job a clear name, like
add_taxoraverage_price. - You want to split a long program into readable parts.
A good function does one job, and its name says which job. If a function both calculates and prints, split it. The calculating part uses return, and the calling code decides what to print.
What mistakes do beginners make with functions?
- Forgetting to call it.
defonly defines. Withoutgreet(), the body never runs. - Calling without parentheses.
greetpoints to the function but does not run it. - Using print instead of return. You get
Nonewhen you use the result later. - Passing the wrong number of arguments. Calling a two-parameter function with one argument raises a
TypeError. - Calling before defining. Python reads the file from top to bottom, so the
defline must run first, or you get aNameError.
What to read next
- Split a program into functions in the lesson [Functions and return](/learn/python/function-return).
- Learn more about inputs in [Parameters and arguments](/learn/python/parameters-arguments) and [Scope](/learn/python/scope-breaking-into-parts).
- Practice with the project [Unit and currency converter](/projects/unit-converter).
- Next, handle bad input with [Python try except](/blog/python-try-except).
- #python
- #functions
- #def
- #return