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.
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
- return ends the function immediately.
- No return means the result is None.
Code examples
Return ends the call
-2 None
The implicit None
value: 5 None
More examples
Guard clauses
(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:
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
Prefer
When n is 0 no return runs, so the function returns None.
Code after return
Avoid
Prefer
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
- 1Write find_index(items, target) that returns the first index or -1.
- 2Rewrite 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?