What is “return” in Python?

Menard Maranan
3 min readAug 31, 2020

--

Python return Statement Tutorial

What is the return statement, what does it do, and why use it?

These are just some of the many confusion Python Beginners ask (including me) especially when learning Python Functions.

And in this article, we’re gonna clear out everything for good.

So to start with, why should we use the return statement?

The answer is so simple: to return the output.

The output of what?

Answer: The output of Functions.

And so?

Functions are like machines. You can give it an input and it will give you an output. If you don’t return the output, you’ll get nothing.

And in the case of Python, the return statement is the one that spits out or returns the output of a function. If you don’t have a return statement, your function will give you “None” (literally, you’ll get “None”).

Got it?

To make it MORE CLEAR for you, I have this simple diagram that depicts an imaginary machine (and how Functions work).

Python Function is like a Machine

This machine represents a function in Python.

The arguments accept the input for the function, while the codes inside the function process the input, and the return statement (which is the conveyer belt) spits out the output.

Alright, got it?

Great!

NOTE: Not all Python Functions accept arguments (or inputs). There are functions that don’t accept or require arguments.

And to help wrap things up…

The return statement is very important when creating functions if you want to access the output.

If you don’t return the output, you’ll be getting None.

To show my point…

Here's an example function that has no return statement:

# This function gives you the root of a number
def root(base, root=2):
answer = pow(base, (1 / root))
# Let's say we want to know the square root of 9:
print(root(9))

Running this code will give us this output:

None
[Finished in 0.4s]

As you can see, even though we called the function and even printed it, we didn't get the answer for the square root of 9, but instead, Python tells us

None

Because we don't have the return statement in the root function.

So to fix this…

Here's the version 2 of our root function, and at this time, we will now include the return statement so that we can get the answer:

# The root functions tells the root of a number.
def root(base, root=2):
answer = pow(base, (1 / root))
return answer
# Let us find the square root of 9 by using the root function
print(root(9))

Running this program will give us the square root of 9:

3.0
[Finished in 0.3s]

See?

This simple example alone gives us the clarification about the return statement and why use it (I mean it, USE RETURN STATEMENT FOR YOUR FUNCTIONS).

Alright.

So that's the return statement in a nutshell. Hope you learned something from this article.

Make sure to watch the Full Python Return Statement Tutorial here:

FULL PYTHON RETURN TUTORIAL

This video tutorial also has a quiz so that you can really understand the return statement.

This unit is Part of my FREE FULL PYTHON TUTORIAL SERIES (scratch to beginner to intermediate) that gives you the springboard you’ll need to learn the exciting and advanced topics in Python like Machine Learning and Web Development.

If you are ready to learn Python Step-by-step from scratch to beginner to Intermediate, click the link below, finish the units from the very first up to the very last, and get ready to kick it!

I’m ready to become a PYTHON PRO

Do you mind sending a tip?

I put a TREMENDOUS EFFORT into putting all these contents and tutorials online for Free to everyone, but that also means I’m spending a lot of time just to finish them. And your warm help is a BIG THING FOR ME, promise.

Please support me via small Donation

Thank you in advance!

--

--