Module 18: Function Return Values

Return to Dashboard

Knowledge Check

What is the main purpose of the return statement in a Python function?

Consider this function:
def add_numbers(x, y):
    result = x + y
    return result

How would you store the value returned by calling add_numbers(5, 3) into a variable named total?

What happens if a function doesn't have an explicit return statement?

What will be printed by the following code?
def greet(name):
    message = "Hello, " + name
    # No return statement here

result = greet("Alice")
print(result)

Can a function return multiple values in Python? If so, how are they typically returned?

What does the return statement do to the execution flow of a function?

Consider the function:
def check_number(num):
    if num > 10:
        return "Large"
    elif num > 5:
        return "Medium"
    else:
        return "Small"

What is the value of check_number(7)?

Why is it generally better to use return to get a result from a function rather than printing it inside the function?

What value is returned by return without any expression after it (i.e., just return)?

If you have result = calculate_area(10, 5), what does this imply about the calculate_area function?