Blog Post #92: What Happens When You Don’t Return? The None Type

In Post #91, we learned how to use the return statement to send a calculated value back from a function. This is essential for functions that are designed to produce a result that can be stored in a variable or used in further calculations.

But this raises an interesting question: what about the functions we wrote earlier that just performed an action, like printing a message, and didn’t have a return statement? Do they return anything at all?

The answer is yes! In this post, we’ll discover Python’s default return value and learn about the special None type.

The Implicit Return Rule

There is a simple and consistent rule in Python:

Every function in Python returns a value. If you do not write an explicit return statement, the function will automatically return None when it reaches the end.

As a reminder, None is a special built-in value. It is Python’s way of representing the absence of a value, or “nothingness.” We’ve seen it before, for example when using the dictionary .get() method (Post #66) for a key that doesn’t exist. It is also a “Falsy” value, as we learned in Post #24.

Seeing it in Action

Let’s prove this rule with a simple experiment. Here is a function that prints a greeting but has no explicit return statement.

def greet(name):
    """Prints a simple greeting."""
    print(f"Hello, {name}!")

Now, let’s call it and capture whatever it returns into a variable.

# Call the function
return_value = greet("Alice")

# Now, let's inspect the variable
print(f"The return value of the greet function is: {return_value}")
print(f"The type of the return value is: {type(return_value)}")

When you run this code, you will see the following output:

Hello, Alice!
The return value of the greet function is: None
The type of the return value is: <class 'NoneType'>

Let’s trace what happened:

  1. We called greet("Alice").
  2. The function executed its code, printing “Hello, Alice!” to the console.
  3. It reached the end of its indented block without finding a return statement.
  4. Python automatically returned None.
  5. The None value was then assigned to our return_value variable, which we then printed.

Two Kinds of Functions

This helps us think about functions in two main categories:

  1. Functions Called for Their Return Value: These are functions like add_numbers() from our last post. Their main purpose is to compute and return a value that you will use. We almost always assign their result to a variable (e.g., sum_value = add_numbers(5, 3)).
  2. Functions Called for Their Side Effects: These are functions like our greet() function or the built-in print(). Their main purpose is to do something—like print to the screen, save a file, or modify a list. We usually don’t care about their return value because we know it’s just None.

What’s Next?

You now know that every Python function returns something. If you don’t specify a value with an explicit return statement, the function will automatically return None by default. This consistent behavior is a key part of the Python language.

We know how to return one value, and we know what happens when we return nothing. But what if we need to return multiple pieces of information from a single function? As we briefly saw with tuples in Post #61, Python has an elegant way to handle this. In Post #93, we will revisit how to return multiple values at once.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment