def, parameters and return · Python from Zero
How do you build your own reusable tools that take inputs and give back results?
Course contentsWhy you need it
With the robot you used def to name a group of commands. Real functions go further: they take parameters (inputs) and return a value (output), just like len("abc") takes a string and returns 3. Functions let you test pieces separately, reuse them, and keep programs readable.
The intuition
A function is a small machine: something goes in, something comes out.
def area(width, height):
return width * height
print(area(3, 4))widthandheightare parameters: names for the inputs.- In
area(3, 4), 3 and 4 are arguments: the actual values. returnsends a value back to where the function was called and ends the function.area(3, 4)becomes 12.
print only shows a value on the screen; return hands it back so the program can keep using it. A function without return gives back None.
Definition
Parameter and argument
A parameter is a variable named in a function definition; an argument is the value passed to it when the function is called.
Return value
The return statement ends a function and sends a value back to the caller; without it the function returns None.
Examples
A function that returns
Try it yourselfClick through the trace to see the jumps.
def area(width, height):
return width * height
total = area(3, 4) + area(2, 5)
print(total)- L1Define area. Nothing runs yet.
- L4Call area(3, 4): width = 3, height = 4.
width = 3, height = 4 - L2Return 12. area(3, 4) is replaced by 12.
→ 12 - L4Call area(2, 5) → 10. 12 + 10 = 22.
total = 22 - L5Print 22.
22
print is not return
Try it yourselfshout prints but returns nothing, so result is None.
def shout(word):
print(word.upper())
def louder(word):
return word.upper() + "!"
result = shout("hi")
print(result)
print(louder("hi"))- L7shout prints HI, but has no return...
HI - L8...so result is None.
None - L9louder returns "HI!" and print shows it.
HI!
Common mistakes
Printing instead of returning
If other code needs the result, return it. A function that only prints can't be used in a calculation.
Code after return
return ends the function immediately; lines after it in the same block never run.
Quick quiz
1. What does this print?
def f(x):
return x * 2
print(f(3) + f(1))Show the answer
Answer: A
f(3) is 6 and f(1) is 2; 6 + 2 = 8.
2. What does this print?
def g():
print("hi")
result = g()
print(result)Show the answer
Answer: A
g prints hi, but returns nothing, so result is None.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictCopies of values
Type exactly what this program prints.
- ModifyReturn, don't print
squareprints its result, sosquare(4) + 1fails. Change it to return n × n. - WriteEven helpers
Write two functions.
is_even(n)returns True if n is even, otherwise False.count_even(nums)returns how many numbers in the list are even, usingis_even.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Parameter | 参数 | A name in a function definition that receives a value when the function is called. |
| Argument | 实参 | The actual value passed to a function in a call. |
| return | return 返回 | The statement that ends a function and sends a value back to the caller. |
| None | None 空值 | The value a function gives back when it has no return statement. |