Contents
If you’re a Python programmer, you may be wondering how to use the return statement. Here’s a quick explanation of what it does and how to use it.
Checkout this video:
Introduction
Returns in Python allows a function to return one or more values. By default, a function will return None if no value is explicitly returned. To return a value from a function, we use the return keyword.
What is return in Python?
Python’s return statement is used to return a value from a function. It is also used to exit a function and go back to the calling function. The return statement can return multiple values, however, it can only return one value at a time.
The return statement
In Python, the return statement is used to exit a function and return a value. If the return statement is without an expression, then the special value None is returned. A return statement with no arguments is equivalent to return None.
When used with a value, the expression is evaluated and then returned:
>>> def square(x):
… return x*x
If you use a return statement with no arguments, then it will exit the function and return to the caller:
>>> def foo():
… print(“inside foo”)
… return #returns None by default!
>>> print(foo()) #prints “None” as output
None
How to use return in Python?
Using return in a function gives the function a value to return to the caller. Returning values is optional; if a function doesn’t explicitly use return to return a value, the function simply returns None.
A function can have multiple return statements. When the first return statement is reached, the function immediately exits and no further code in the function is executed. Return values can be of any type, including complex types such as lists and dictionaries.
Functions can alsoreturn multiple values by separating the values with commas. When returning multiple values, they are returned as a tuple (a special type of list).
Examples
Returns in Python
In Python, the return statement is used to exit a function and return a value. It can be used in the following ways:
-Return with no value
-Return with a value
-Return with multiple values
Return with no value
If you don’t want to return a value from a function, you can use the return statement without any value. This will cause the function to exit and the program will continue to execute the code after the function call.
Example:
“`py
def my_func():
return
x = my_func() # x will be None
print(x) # output: None
“`
Tips
Returns are one of the basic tools that we use in functions in Python. They allow us to return a value from a function, and can be used to terminate a function prematurely. Let’s take a look at how they work.
When we return a value from a function, the function implicitly stops executing at that point. For example, if we have a function that takes two parameters and returns the sum of those parameters:
“`python
def add(a, b):
return a + b
“`
The function will stop executing as soon as it reaches the return statement, and will return the value of `a + b` to the caller. If we want to continue executing the function after a return statement, we can use the `return` statement with no arguments:
“`python
def doSomething():
# Do something here…
return # End execution of the function and return to caller.
# This code will never execute!
print(“This code will never execute!”)
“`
Conclusion
We have seen how to use the return statement in Python. We have also seen how to use it with functions and methods. Finally, we have seen how to use it with generator functions.
Further reading
-PEP 8 — Style Guide for Python Code: https://www.python.org/dev/peps/pep-0008/
-PyCharm Blog Entry on the return Statement in Python: https://blog.jetbrains.com/pycharm/2017/03/python-tips-5-different-uses-return-statement/