Lesson 9.2: Parameters and Return Values
What You'll Learn
- The difference between parameters and arguments
- How to define functions with one or more parameters
- How positional arguments work
- How to use the
returnstatement - How to return multiple values
- What
Nonemeans when a function has no return statement
1. Parameters vs Arguments
These terms are often used interchangeably, but they have a specific meaning:
Argument: The actual value you pass to the function when you call it.
# 'name' is a PARAMETER (in the definition) def greet(name): print(f"Hello, {name}!") # "Alice" is an ARGUMENT (in the call) greet("Alice")
2. Functions with Multiple Parameters
Separate parameters with commas:
def add(a, b): """Add two numbers and print the result.""" result = a + b print(f"{a} + {b} = {result}") add(3, 5) add(10, 20)
Positional Arguments
By default, arguments are matched to parameters by position:
def describe_pet(animal, name): """Describe a pet.""" print(f"I have a {animal} named {name}.") describe_pet("dog", "Rex") describe_pet("cat", "Whiskers") # Order matters! This gives a wrong result: describe_pet("Rex", "dog")
TypeError.
3. The return Statement
So far, our functions have only printed output. But often you want a function to compute a value and give it back so you can use it later. That is what return does:
def add(a, b): """Return the sum of two numbers.""" return a + b # The returned value can be stored in a variable result = add(3, 5) print(result) # Or used directly in expressions print(add(10, 20) * 2)
return is executed.
return vs print
Understanding the Difference
# This function PRINTS (shows output but returns nothing) def add_print(a, b): print(a + b) # This function RETURNS (sends a value back) def add_return(a, b): return a + b # With print: you see output but can't use the value x = add_print(3, 5) print(f"x is: {x}") # With return: you can store and reuse the value y = add_return(3, 5) print(f"y is: {y}")
Notice that add_print displays 8 but returns None. The add_return function gives back 8 as a usable value.
4. Returning Multiple Values
Python functions can return multiple values as a tuple:
def get_min_max(numbers): """Return the minimum and maximum of a list.""" return min(numbers), max(numbers) # Unpack into separate variables low, high = get_min_max([4, 1, 7, 2, 9]) print(f"Min: {low}, Max: {high}") # Or keep as a tuple result = get_min_max([4, 1, 7, 2, 9]) print(result)
def divide_with_remainder(a, b): """Return quotient and remainder.""" quotient = a // b remainder = a % b return quotient, remainder q, r = divide_with_remainder(17, 5) print(f"17 / 5 = {q} remainder {r}")
5. Functions That Return None
If a function has no return statement (or just says return with no value), it returns None:
def say_hello(name): print(f"Hello, {name}!") # no return statement result = say_hello("Alice") print(f"Return value: {result}") print(type(result))
None by default.
6. Early Return
You can use return to exit a function early based on a condition:
def divide(a, b): """Divide a by b, with error checking.""" if b == 0: print("Error: Cannot divide by zero!") return None return a / b print(divide(10, 3)) print(divide(10, 0))
Check Your Understanding
Question: What does this code print?
def mystery(x): return x * 2 a = mystery(5) b = mystery(a) print(b)
Answer: 20
mystery(5) returns 10 (stored in a). Then mystery(10) returns 20 (stored in b).
Try It Yourself
- Write a function
square(n)that returns the square of a number - Write a function
is_even(n)that returnsTrueif n is even,Falseotherwise - Write a function
full_name(first, last)that returns the full name as a single string - Write a function
circle_stats(radius)that returns both the area and circumference
Key Takeaways
- Parameters are defined in the function header; arguments are the values passed when calling
- Positional arguments must be passed in the correct order
returnsends a value back to the caller and ends the function- Functions without a
returnstatement returnNone - Functions can return multiple values as a tuple
returngives back data;print()just displays text