Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

return

The return statement exits the current function call and provides its result. With no expression, it returns None; generator and asynchronous-generator functions have specialized completion rules.

How it works

Within a function, return ends the current call and supplies either the evaluated expression list or None when no expression is present. It also has special completion behavior for generator functions, and a finally clause runs before control actually leaves a try statement.

Syntax

return [expression_list]

Parameters

NameTypeDescription
expression_listexpression listOptional expression list whose evaluated result becomes the function's return value.

Returns

The evaluated expression list, or None when the expression list is omitted.

Key points

  • The statement consists of the keyword return followed optionally by an expression list.
  • An expression is evaluated for the result; omitting it returns None.
  • Execution leaves the current function call and passes its result to the caller.
  • A generator uses return to signal completion rather than to provide an ordinary function result.
  • A generator's returned expression is stored on the resulting StopIteration exception.
  • An async generator can use an empty return to signal that iteration has ended.

Common mistakes

  • A return statement is valid inside a function, but not inside a class definition nested within that function.
  • An asynchronous generator may use an empty return to finish, but it cannot return a value.
  • A finally clause still executes before a return fully exits the function.

Example: Returning a value or None

def divide(total: float, count: int) -> float | None:
    if count == 0:
        return
    return total / count

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

The first call returns a calculated value; the second uses an empty return and therefore produces None.

When to use it

Return a calculation

Provide a computed result from a function to its caller.

Early exit

Stop processing early after a condition is met.

End generator iteration

Finish a generator and optionally attach a value to StopIteration.

Interview questions

QWhat does return do when it has no expression?

return ends the current function call. With an expression list, that value is evaluated and returned; without one, the returned value is None.

Follow-up: What happens if return occurs in try with finally? The finally clause executes before the function is actually left.

QHow does return behave in a generator function?

In a generator, return marks the generator as finished and causes StopIteration. If it supplies a value, that value becomes the StopIteration.value attribute.

Follow-up: How does this differ for an asynchronous generator? An empty return raises StopAsyncIteration, while a value-returning return is a syntax error.

QWhere can return appear, and what is its syntax?

The statement has the form return [expression_list] and may occur syntactically nested in a function definition, but not within a nested class definition.

Follow-up: What does an omitted expression mean operationally? Python substitutes None as the return value.

  • yield
  • raise
  • StopIteration
  • StopAsyncIteration
  • try
  • finally

Source: 7. Simple statements. Checked 2026-10-16.