Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Functions with no Return | Functions
Introduction to Python (copy)
course content

Course Content

Introduction to Python (copy)

Introduction to Python (copy)

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

book
Functions with no Return

Functions without a return statement are helpful when you want to structure your code into reusable sections that perform tasks like printing messages, modifying data, or executing actions within your program.

Note

In Python, every function returns a value. If a function doesn't explicitly include a return statement, it will automatically return None.

Let's see how Alex demonstrates the creation and use of 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

Note

The order in which data and functions are declared doesn't matter. The only important rule is that a function must be defined before it is called.

Modifying a Data Structure

Developers often need to create functions that modify a data structure, like a list or dictionary, without returning a value.

For instance, the update_inventory() function adjusts inventory levels based on the items_sold. Since the function modifies the inventory dictionary directly, there's no need to return anything:

12345678910111213141516171819202122232425
# Define the function that adjusts inventory levels def update_inventory(inventory, items_sold): # Iterate over each item in the dictionary for product, quantity_sold in items_sold.items(): # Decrease the inventory by the quantity sold for each product inventory[product] -= quantity_sold # Inventory dictionary inventory = { "apples": 50, "bananas": 75, "oranges": 100 } # Items sold dictionary items_sold = { "apples": 5, "oranges": 15 } # Update the inventory based on items sold update_inventory(inventory, items_sold) # Display the updated inventory print("Updated inventory:", inventory)
copy

Calling Another Function

It's common to create functions that monitor specific conditions and trigger other functions when needed.

For example, the check_stock_levels() function checks if any product's stock level drops below a set threshold. If so, it calls the restock() function to order more inventory.

This approach doesn't require returning a value, as the main goal is to initiate the restocking process:

12345678910111213141516171819202122232425
# Dictionary representing the current stock of products inventory = { "apples": 17, "bananas": 75, "oranges": 2, "grapes": 50 } # Function to restock items that have low stock levels by adding a specified amount def restock(product, inventory, restock_amount): inventory[product] += restock_amount print(f"Restock order placed for {product}. New stock level: {inventory[product]} units.") # Function to check which items are below the stock threshold and trigger the `restock` function def check_stock_levels(inventory, threshold): for product, quantity in inventory.items(): if quantity < threshold: # If the stock is below the threshold, call the `restock` function to add 50 units restock(product, inventory, 50) # Checking the stock levels for all products in the inventory with a threshold of 30 units check_stock_levels(inventory, 30) # Display the final inventory after restocking print("Final inventory status:", inventory)
copy
question mark

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?

How can we improve it?

Thanks for your feedback!

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