Skip to content

Module 9 · Functions — topic 11 of 19

Return statements

Sending results back with return, returning early from guard clauses, the implicit None, and the difference between returning and printing.

Beginner 8 min read·Lesson 136 of 306 in Python Programming

Learning objectives

  • Return values from functions and use them in the caller
  • Use early returns to simplify logic
  • Recognise functions that accidentally return None

Definition

A return statement ends the current function call immediately and makes the call expression evaluate to the given value; a function that finishes without return (or uses a bare return) returns None.

Introduction

return is how a function hands its answer back. The moment Python reaches a return, the function stops — no lines after it in that call are executed.

Why it matters

Functions that return values can be combined, tested and reused. Accidentally returning None is a very common source of TypeErrors such as 'unsupported operand type(s) for +: NoneType and int'.

Detailed explanation

return expression evaluates the expression and ends the call; the caller receives that value.

A function can have several return statements, for example one per case. Only the first one reached runs.

Reaching the end of the body, or writing a bare return, returns None.

Early returns (guard clauses) handle special cases first and keep the main logic unindented.

Printing is for showing information to people; returning is for passing information to code. A function can do both, but most should only return.

Syntax

Python
def grade(marks):
if marks < 0 or marks > 100:
return "invalid" # early return
if marks >= 90:
return "A"
return "B or below"
  • return ends the function immediately.
  • No return means the result is None.

Code examples

Return ends the call

Python
def first_negative(numbers):
for n in numbers:
if n < 0:
return n
return None
print(first_negative([4, 7, -2, -9]))
print(first_negative([1, 2]))
Output
-2
None

The implicit None

Python
def show(x):
print("value:", x)
result = show(5)
print(result)
Output
value: 5
None

More examples

Guard clauses

Python
def shipping_cost(weight_kg, distance_km, express=False):
if weight_kg <= 0 or distance_km <= 0:
return 0
if weight_kg > 30:
return None # too heavy to ship
cost = 40 + weight_kg * 12 + distance_km * 0.5
if express:
return round(cost * 1.5, 2)
return round(cost, 2)
for args in [(2, 100), (2, 100, True), (35, 10), (0, 50)]:
print(args, "->", shipping_cost(*args))
Output
(2, 100) -> 114.0
(2, 100, True) -> 171.0
(35, 10) -> None
(0, 50) -> 0

Step-by-step explanation

  • Invalid inputs are handled first and return 0 immediately.
  • Overweight parcels return None, meaning 'can't ship'.
  • Only valid parcels reach the cost formula: 40 + 24 + 50 = 114.
  • The express branch returns early with a 50% surcharge.

Expected output

Running “Guard clauses” prints exactly:

TextOutput
(2, 100) -> 114.0
(2, 100, True) -> 171.0
(35, 10) -> None
(0, 50) -> 0

Practical applications

  • Validation functions return True/False or an error message.
  • Lookup functions return None when nothing is found.

Best practices

  • Return values; let the caller decide whether to print.
  • Use guard clauses for invalid input.
  • Be consistent: if some paths return a value, all paths should.

Common mistakes

Forgetting return in one branch

Avoid

def sign(n):
if n > 0:
return "positive"
elif n < 0:
return "negative"

Prefer

def sign(n):
if n > 0:
return "positive"
if n < 0:
return "negative"
return "zero"

When n is 0 no return runs, so the function returns None.

Code after return

Avoid

def f():
return 1
print("done")

Prefer

def f():
print("done")
return 1

Statements after return in the same block never execute.

Important notes

  • Returning None to signal 'nothing found' is common; raising an exception is better when the situation is an error.

Key takeaways

  • return gives a value back and stops the call.
  • Without return, a function returns None.
  • Early returns simplify branching logic.

Try it yourself

  1. 1
    Write find_index(items, target) that returns the first index or -1.
  2. 2
    Rewrite a deeply nested if/else function using guard clauses.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic