🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

return Statement

AI & DATA SCIENCE // return-statement

The return statement immediately exits a function and optionally sends a value back to the caller.

Syntax

def func():
    return value

result = func()

Deep Dive Course

return does two things at once: it stops the function's execution immediately, skipping any remaining code in its body, and it hands a value back to wherever the function was called from. A function with no return statement, or a bare return with no value, implicitly returns None. You can return multiple values by returning a tuple, which the caller then typically unpacks into separate variables.

1Understanding return Statement

return does two things at once: it stops the function's execution immediately, skipping any remaining code in its body, and it hands a value back to wherever the function was called from. A function with no return statement, or a bare return with no value, implicitly returns None. You can return multiple values by returning a tuple, which the caller then typically unpacks into separate variables.

💡

A bare return, with no value, inside a function is a valid, useful way to exit early — for example, in a guard clause that stops processing when some precondition isn't met — and it implicitly returns None just like falling off the end of the function would.

editor.html
def square(x):
    return x * x

result = square(5)
print(result)
localhost:3000

2Practical Example

Here is a real-world application of return Statement showing how it is used in production Python code.

editor.html
def divide(a, b):
    if b == 0:
        return None
    return a / b

print(divide(10, 2))
print(divide(10, 0))
localhost:3000

3Best Practices

Follow these guidelines when working with return Statement:

1. Use an early return as a guard clause to exit a function as soon as its precondition fails, instead of nesting the rest of the logic in an else block

2. Return a tuple to hand back multiple related values from a single function, rather than a custom container class

3. Keep a function's return type consistent across all its code paths, so callers don't have to guard against sometimes getting None and sometimes getting a real value unexpectedly

⚠️

Tip: A bare return, with no value, inside a function is a valid, useful way to exit early — for example, in a guard clause that stops processing when some precondition isn't met — and it implicitly returns None just like falling off the end of the function would.

editor.html
def square(x):
    return x * x

result = square(5)
print(result)
localhost:3000

Examples

Example 01Basic Usage
def square(x):
    return x * x

result = square(5)
print(result)
Example 02Advanced Example
def divide(a, b):
    if b == 0:
        return None
    return a / b

print(divide(10, 2))
print(divide(10, 0))

Best Practices

  • Use an early return as a guard clause to exit a function as soon as its precondition fails, instead of nesting the rest of the logic in an else block
  • Return a tuple to hand back multiple related values from a single function, rather than a custom container class
  • Keep a function's return type consistent across all its code paths, so callers don't have to guard against sometimes getting None and sometimes getting a real value unexpectedly

Interview Question

What does a function return if it reaches the end of its body without hitting a return statement?

Hint: Think about the implicit behavior, not an error.

It returns None automatically — Python doesn't require every code path to explicitly return a value, and a function that simply runs out of statements, or that hits a bare return with nothing after it, implicitly evaluates to None for the caller. This is a common source of bugs when a function is expected to always return something meaningful but has a code path that falls through without an explicit return.

Exercises

MediumPractice using return Statement in a real scenario.
View Solution
def square(x):
    return x * x

result = square(5)
print(result)

Frequently Asked Questions

What does a function return if it reaches the end of its body without hitting a return statement?

It returns None automatically — Python doesn't require every code path to explicitly return a value, and a function that simply runs out of statements, or that hits a bare return with nothing after it, implicitly evaluates to None for the caller. This is a common source of bugs when a function is expected to always return something meaningful but has a code path that falls through without an explicit return.

Related Functions

def-keywordnonetypetuples