Python return statement
The python return statement is used inside a function. Because variables only exist within the local function scope, return will give any variable outputs to the caller of the function.
The return statement can only be used inside a function.
Every function returns a value, by default. If there are no return statements, then it returns None.
If the return statement contains an expression then it is first evaluated and then the value is returned.
Related course: Complete Python Programming Course & Exercises
What does the return statement do?
The return statement terminates the function execution.
- A function can have multiple return statements, but only one is executed.
- A function can return multiple types of values (unlike C or Java).
- A single return statement can return multiple values
Python return Statement Example
The program below has one function sum(x,y)
. The parameters it takes are x
and y
. Anything that happens in the function stays in the funciton, unless return is used.
def sum(x,y)
result = x + y
return result
function_output = sum(2,3)
print(function_output)
You can write this shorter as:
def sum(x,y):
return x + y
print(sum(2,3))
Every Function returns Something
Even if you did not write return in your function, Python returns something.
def hello():
pass
print(hello())
This outputs:
None
So the function returned None
.
What happens when the return statement has nothing?
If you create a function without a return statement, what happens?
def hello():
print("Hello world")
function_output = hello()
print(function_output)
It returns the None
type.
Python Functions can have multiple return statements
A function is not limited to one return statement, it can have several. In this case it's used in combination with an if statement
and depending on the value either return is called.
def set_age(age):
if age < 0:
return "Error"
else:
return age
set_age(5)
This is by no means limited to two return statements and raise Exception
is usually used instead of returning an error string.
def can_drive(age):
if age < 0:
raise Exception
elif age < 18:
print("Minimum age")
elif age >= 18 and age <= 70:
print("Can drive")
else:
print("Driving not allowed")
can_drive(0)
can_drive(5)
can_drive(25)
can_drive(88)
Python Function can return multiple types of values
Python functions can return all kinds of data types like integers, floats, complex numbers, lists, tuples etc.
def f(x):
if x == 0:
return 1
elif x == 1:
return 1.5
elif x == 2:
return 1+2j
elif x == 3:
return [1,2,3]
elif x == 4:
return (1,2,3)
Returning Multiple Values in a single return Statement
Unlike C, C++ or Java, Python functions can return more than one value. So if you have more than one variable in your function, Python can return all of them.
def mul(x,y):
return x*y, x, y
result,x,y = mul(5,6)
Python return statement with finally block
The return statement can also be used in a try-except
block. In other programming languages this is called try-catch.
def f():
try:
return 1
except:
return 2
finally:
return 3
output = f()