Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Functions with no `return` | Functions
Introduction to Python Video Course
course content

Course Content

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

Functions with no `return`

In Python, all functions return a value. If a function doesn't include a return statement, it automatically returns None. Functions without a return statement are useful when you want to organize your code into reusable blocks that execute tasks like printing information, modifying data structures, or triggering actions in your program. Understanding when and how to use such functions is essential for writing clear and efficient Python code.

Now, let's watch as Alex demonstrates how to create and use functions that don’t return a value:

The best way to understand how we use functions with no return statement is to see them in action, so let’s look at some examples.

Printing Information to the Console

Sometimes, the purpose of a function is simply to notify the user of a specific event or outcome by printing information to the console.

For example, there’s no need to return a value in the total_sales function because its primary role is to perform a calculation and display the result immediately:

1234567
# Prices of items sold today prices = [12.99, 23.50, 4.99, 8.75, 15.00] def total_sales(prices): print(f"Today's total sales: $", sum(prices)) total_sales(prices)
copy

Modify a Data Structure

Often, developers need to define functions that update or modify a data structure, such as a list or dictionary, without needing to return any value.

For example, the update_inventory function adjusts the inventory levels based on the items_sold. Since the function directly updates the inventory dictionary and provides immediate feedback through print statements, there’s no need for it to return anything:

1234567891011121314151617181920212223242526
# Inventory dictionary inventory = { "apples": 50, "bananas": 75, "oranges": 100 } # Items sold dictionary items_sold = { "apples": 5, "bananas": 10, "oranges": 15 } def update_inventory(inventory, items_sold): for product, quantity_sold in items_sold.items(): inventory[product] -= quantity_sold print(f"Sold {quantity_sold} {product}") print(f"New inventory level for {product}: {inventory[product]}") print("") # Updating inventory based on items sold update_inventory(inventory, items_sold) # Final inventory status print("Updated inventory:", inventory)
copy

Triggering Another Function

It’s common to define functions to monitor certain conditions and trigger other functions when necessary.

For instance, the check_stock_levels() function checks if any product’s stock level falls below a threshold. If it does, the function triggers the reStock function to order more stock. This process operates without needing to return a value, as the primary task is to initiate the restocking action:

12345678910111213141516171819202122232425
# Inventory dictionary inventory = { "apples": 4, "bananas": 75, "oranges": 2, "grapes": 50 } def check_stock_levels(inventory, threshold): for product, quantity in inventory.items(): if quantity < threshold: print(f"Inventory for {product} is running low. Triggering reStock function.") # If true, trigger the reStock function with a restock_amount = 50 reStock(product, inventory, 50) def reStock(product, inventory, restock_amount): inventory[product] += restock_amount print(f"Order placed for more {product}. New stock level: {inventory[product]} units.") print("") # Running a stock check on all products in the inventory dictionary with a threshold of 5 check_stock_levels(inventory, 5) # Final inventory status print("Final inventory status:", inventory)
copy

In Python, every function returns a value. If a function doesn't have a return statement, what does it return?

Select the correct answer

Everything was clear?

Section 6. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt