returnEasy Examples

Exits a function and optionally sends a value back to the caller

Return a value from a function

Using return to send results back to the caller.

python
def add(a, b):
    return a + b

result = add(3, 4)
print(result)

def is_even(n):
    return n % 2 == 0

print(is_even(4))
print(is_even(7))
Expected Output
7
True
False

return sends a value back to the caller and exits the function immediately. Without return, a function returns None.

Multiple return paths

Functions can return from different points.

python
def absolute_value(x):
    if x >= 0:
        return x
    return -x

print(absolute_value(5))
print(absolute_value(-3))

def find_first_even(numbers):
    for n in numbers:
        if n % 2 == 0:
            return n
    return None

print(find_first_even([1, 3, 4, 6]))
print(find_first_even([1, 3, 5]))
Expected Output
5
3
4
None

A function can have multiple return statements. Execution stops at the first one reached.

Want to try these examples interactively?

Open Easy Playground