Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is return in Python

Understanding the Concept of Return in Python

When you're starting out with programming, one of the concepts you'll encounter is the return statement. In Python, return is a key part of functions, which are reusable blocks of code designed to perform a specific task. To understand return, think of a function as a small factory where something goes in, a process happens, and then something comes out. The return statement is like the delivery truck that brings the final product out of the factory to you.

What Does Return Do?

In Python, the return statement is used within a function to send a result back to the caller. This result can be any Python object, such as a number, a string, a list, or even another function. When return is executed, the function ends immediately, and the value specified in the return statement is passed back to wherever the function was called.

Here's a simple example:

def add_numbers(a, b):
    result = a + b
    return result

sum = add_numbers(3, 5)
print(sum)  # This will print 8

In this code, add_numbers is a function that takes two numbers, adds them together, and uses return to give back the result. When we call add_numbers(3, 5), the function returns 8, which is then stored in the variable sum.

Functions Without Return

Not all functions need to have a return statement. If a function doesn't need to send back any value, it can simply perform its task without returning anything. In Python, if a function doesn't explicitly return a value, it implicitly returns None, which is a special type that represents the absence of a value.

Here's an example of a function that doesn't return anything:

def print_greeting(name):
    greeting = f"Hello, {name}!"
    print(greeting)

print_greeting("Alice")  # This will print "Hello, Alice!" to the console

In this case, print_greeting just prints a greeting to the console and doesn't return anything.

Multiple Return Points

A function can have multiple return statements, but only one will ever be executed. As soon as a return statement is hit, the function ends. This can be useful for handling different conditions within a function.

Consider this function that checks if a number is positive, negative, or zero:

def check_number(num):
    if num > 0:
        return "Positive"
    elif num < 0:
        return "Negative"
    else:
        return "Zero"

print(check_number(10))  # Positive
print(check_number(-5))  # Negative
print(check_number(0))   # Zero

Returning Multiple Values

Python has a neat feature where you can return multiple values from a function using tuples. A tuple is like a list that cannot be changed after it's created (it's "immutable"). When you return multiple values, Python packs them into a tuple automatically.

Here's how you can return multiple values:

def get_full_name(first_name, last_name):
    full_name = first_name + " " + last_name
    return first_name, last_name, full_name

first, last, full = get_full_name("John", "Doe")
print(full)  # John Doe

The None Pitfall

One common pitfall for beginners is forgetting to return a value from a function. If you forget the return statement, the function will return None, which might not be what you intended.

def subtract_numbers(a, b):
    result = a - b
    # Oops! We forgot to return the result

difference = subtract_numbers(10, 5)
if difference is None:
    print("The function didn't return anything!")
else:
    print(difference)

Using Return Intuitively

Think of the return statement as the answer to a question. When you call a function, you're asking a question, like "What is 3 plus 5?" or "Is this number positive or negative?" The return statement provides the answer.

Conclusion: The Power of Return

In the world of Python programming, the return statement is like the grand finale of a fireworks show. It's the moment when the function completes its dazzling display of logic and delivers the final product of its computation back into the hands of the caller. By mastering the use of return, you can write functions that not only execute tasks but also communicate results, making your code more modular and powerful.

As you continue your journey in programming, remember that the return statement is a tool in your belt, ready to convey the fruits of your functions' labor. Use it wisely, and you'll find that it opens up a world of possibilities for solving problems elegantly and efficiently. With each function you write, you're not just coding; you're crafting a story where return delivers the punchline, the moral, or the happy ending. Happy coding!