Coding/Learn/python/return values
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnReturn Values & Data Flow
30 / 32 0%
Lesson 30 of 32
8 mins read

Return Values & Data Flow

PreviousNext Lesson
In Plain English

Printing a result just splashes it on the screen for a human to read. Returning a result hands the computed value back to your program so other parts of your code can store it, do math with it, or pass it into other functions.

The Mental Model

"Calling print() inside a function is like a chef shouting the order name in the kitchen. Using return is like the chef placing the prepared meal on your tray so you can eat it or take it home."

Deep Dive: How It Works

The return statement terminates the function immediately and passes the specified value back to the caller.

Assigning return values: result = square(5) captures the returned 25 and stores it in the result variable.

Functions without an explicit return statement automatically return None when they finish executing.

Returning multiple values: Python lets you return multiple comma-separated values (e.g. return width, height), which are packed into a tuple.

Syntax Blueprint

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

total = add(10, 20)  # total is 30

return immediately exits the function and delivers the result value to the caller.

PointCore Rules to Remember

Point star
return Hands Back Data: Allows callers to store results in variables for further use.
Point star
return Stops Execution: Any code inside the function written after a return line will never execute.
Point star
print vs return: print shows text to human eyes; return passes real data to the program.

Common Beginner Traps & How to Fix Them

Mistake:Putting code after a return statement

Why it happens: return exits the function instantly. Code below it becomes unreachable dead code.

How to fix: Ensure return is the final action of your logical branch.

Pro Tip: You can use an empty return statement to exit early from a function if an error condition is detected.

Live Interactive Example

Hit Run Code to see it live
Calculating and Chaining Return Values
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: calculate_tax returned 8.0, which was passed into calculate_total to produce the final 108.0.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Write a function that returns a value

  • PointDefine a function square(n) that returns n * n.
  • PointStore the result of square(9) in a variable result and print result.
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates