Learn Without Walls
← Previous Lesson Lesson 2 of 4 Next Lesson →

Lesson 9.2: Parameters and Return Values

What You'll Learn

1. Parameters vs Arguments

These terms are often used interchangeably, but they have a specific meaning:

Parameter: A variable listed in the function definition. It is a placeholder for the data the function will receive.

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")
Hello, 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)
3 + 5 = 8 10 + 20 = 30

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")
I have a dog named Rex. I have a cat named Whiskers. I have a Rex named dog.
Watch Out: The number of arguments must match the number of parameters. Too few or too many will raise a 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)
8 60
return: A statement that ends the function and sends a value back to the caller. The function immediately stops when 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}")
8 x is: None y is: 8

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)
Min: 1, Max: 9 (1, 9)
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}")
17 / 5 = 3 remainder 2

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))
Hello, Alice! Return value: None <class 'NoneType'>
None: Python's special value meaning "no value" or "nothing." Functions that do not explicitly return a value return 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))
3.3333333333333335 Error: Cannot divide by zero! None

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

  1. Write a function square(n) that returns the square of a number
  2. Write a function is_even(n) that returns True if n is even, False otherwise
  3. Write a function full_name(first, last) that returns the full name as a single string
  4. Write a function circle_stats(radius) that returns both the area and circumference

Key Takeaways

← Previous: Defining Functions Next: Default & Keyword Arguments →