Functions — parameters and return values
In the previous lesson you learned how to write and call a function.
Now we go a step further: we will learn how functions can take data
(parameters) and return a result with the keyword return.
This is very important because it lets us use the same function with different values, without writing new code.
1. What are parameters?
Parameters are variables that a function gets when we call it. You can imagine them as entrance doors through which data comes into the function.
When we call a function, we pass it arguments — the actual values that go into the parameters.
2. Example: adding with parameters
# add.py
def add(a, b):
return a + b
result = add(3, 5)
print("3 + 5 =", result)
# In Thonny try other values too:
# add(10, 20)
How does this code work?
aandbare the parameters of the function- 3 and 5 are the arguments we pass to the function
returnreturns the result of the function call- The returned value is stored in the variable
result
3. What does return mean?
The keyword return:
- returns a value from the function
- stops the execution of the function
- lets the result be used later in the program
Without return, a function only runs its code but does not return a result.
4. A function with one parameter
def square(x):
return x * x
print(square(4)) # 16
print(square(10)) # 100
5. A function without parameters
def message():
return "Hello from the function!"
print(message())
The difference between functions with and without a return value
In Python there are two common kinds of functions:
- functions that do not return a value
- functions that return a value with
return
1. A function without a return value
This function only does something (most often prints to the screen), but you cannot keep a useful result from it.
def greet():
print("Hello!")
greet()
This function:
- does not use
return - returns
None– the result cannot be used further
x = greet() # x will be None
2. A function with a return value
This function calculates something and returns the result, which you can use later in the program.
def add(a, b):
return a + b
result = add(2, 3)
print(result)
This function:
- uses
return - returns a value
- can be stored in a variable
A side-by-side comparison
| Without return | With return |
|---|---|
| Prints a message | Calculates and returns a value |
| Uses print() | Uses return |
| Returns None | Can be stored in a variable |
Remember
If you only want something to be shown → use print()
If you want something to be calculated and used later → use return
6. Mini tasks
Task 1 (basic)
Write a function greet(name) that returns the message:
Hello, name!
Task 2 (medium)
Write a function is_even(n) that returns True if the number is even,
and False if it is not.
Task 3 (for talented students)
Write a function maximum(a, b) that returns the bigger of two numbers.
# task_maximum.py
def maximum(a, b):
if a > b:
return a
else:
return b
print(maximum(7, 10)) # expected 10
7. Why are functions with return important?
Functions that return values are the basis of:
- mathematical calculations
- games and scoring
- working with sensors (MicroPython)
- bigger and more serious programs
In the next lesson you will learn what happens to variables inside a function and how Python remembers their values.
Extra practice tasks — parameters and return values (Python)
These tasks are made to help you practise functions that take data and return results (return values).
- A square root and formatted output
Write a function
square_root(x)that returns the square root of the numberx. Test it in the main program and print the result nicely.def square_root(x): # return sqrt(x) without the math module (for example x**0.5) pass print(square_root(25)) # 5.0 - A greeting with a message
The function
greeting_message(name, message)should return a joined message: for example "Hello Ana! Welcome!" — without a directprint()in the function.def greeting_message(name, message="Hello"): pass print(greeting_message("Ana")) print(greeting_message("Mark", "Hi")) - An average mark
Write a function that takes a list of marks and returns the average value. Test it in the main program with several lists.
def average_mark(marks): # return the average pass print(average_mark([5, 4, 3, 5])) - Returning several values
The function
min_max(values)returns the smallest and the biggest number from a list (as a tuple).def min_max(values): pass smallest, largest = min_max([10, 2, 7, 14]) print(smallest, largest) - Calculating perimeter and area (several parameters)
Write a function
rectangle(a, b)that returns the perimeter and the area of a rectangle — first the perimeter, then the area.def rectangle(a, b): pass p, s = rectangle(5, 3) print(p, s) # 16 15 - Default parameters
Write a function
greet(name, greeting="Hello")that uses the default greeting if the second parameter is not passed.def greet(name, greeting="Hello"): pass print(greet("Luke")) print(greet("Sara", "Hi")) - Quotient and remainder
The function
divide(a, b)should return both the result of the division and the remainder (as a tuple). Test it with different numbers.def divide(a, b): pass q, r = divide(17, 5) print(q, r) # 3 2 - A calculator with a return value
Write a function
calc(a, b, op)that returns the result for the operations '+', '-', '*', '/' (take care of division by zero).def calc(a, b, op): pass print(calc(8, 3, '+')) print(calc(8, 0, '/')) - Filtering even numbers
A function
evens(values)that returns a new list with only the even numbers from the given list.def evens(values): pass print(evens([1,2,3,4,5,6])) - Parameters by name (keyword arguments)
Test the function
greet_kw(name=None, message=None)so that it also works if you call it with the names of the parameters, for examplegreet_kw(message="Hi", name="Mila").def greet_kw(name=None, message=None): pass print(greet_kw(message="Hi", name="Mila"))
Bonus challenge: combine functions — make an application that asks the user to enter a list of numbers, and then uses one function to calculate the average, another to calculate the min/max, and a third to print a nice message with the results.
Extra explanations: parameters and return values
In this section we deepen our understanding of how functions work in Python and deal with common doubts that beginners have.
1. The difference between print() and return
One of the most common mistakes is mixing up print() and return.
print()is only for printing to the screenreturnlets a function return a value that we can use later
def add(a, b):
print(a + b)
result = add(2, 3)
print(result)
Although the number 5 is printed on the screen, the variable result has no value that can be used (it is None).
The correct approach:
def add(a, b):
return a + b
result = add(2, 3)
print(result)
2. What happens if a function has no return?
If a function does not contain return, Python automatically returns the value None.
def test():
x = 10
print(test())
The result will be:
None
This is important to know because using None in further calculations can lead to errors.
3. Functions that return different types of data
A function does not have to return only one number or text — it can return:
- a number (
int,float) - text (
str) - a list
- a tuple (several values)
def stats(a, b):
total = a + b
difference = a - b
return total, difference
t, d = stats(10, 3)
print(t, d)
Here the function returns two values at once, which Python packs into a tuple.
4. What if we pass a parameter of the wrong type?
Python does not check types in advance, so the error happens only while the program runs.
def add_one(x):
return x + 1
print(add_one("5"))
This code causes an error because Python cannot add a string and a number.
Good practice is:
- clear names for functions and parameters
- checking the type with
isinstance()(in more advanced examples)
5. How to think about a function – a flowchart
You can imagine a function as a small "machine process":
- Input → parameters
- Processing → the body of the function
- Output → the return value (
return)
If there is no return, the output is automatically None.
Thinking this way helps when writing more complex programs and mini projects.
Hidden mini quiz — parameters and return values
Check your understanding of parameters, the return statement and the values returned from functions.
Open the quiz
Mini quiz: parameters and return
1. What does the following function return?
def total(a, b):
return a + b
2. What will be printed?
def f(x):
return x * 2
print(f(3))
3. How many parameters does the function have?
def test(a, b, c):
return a + b + c
4. What happens after the return statement?
5. What does a function return if it has no return?
Tasks for independent work — parameters and return values (7th and 8th grade)
These tasks check your understanding of parameters, return values and combining several functions. The harder tasks have a short hint, but no solution.
- Doubling a number
Write a function that takes one number and returns its double.
- The area of a square
Write a function that takes the length of the side of a square and returns its area.
- The number of digits
Write a function that takes a whole number and returns how many digits that number has.
Hint: you can turn the number into a string or divide by 10 repeatedly.
- Several return values
Write a function that takes two numbers and returns their sum, difference and product.
Hint: think about how Python returns several values from a function.
- Competition level — analysing a list
Write a function that takes a list of whole numbers and returns how many positive numbers, negative numbers and zeros there are in the list.
Hint: use three counters and return them together. The solution should be clean and clear.
