In this section we will learn what the return statement is and how to use it in Python.
Note: we’re assuming you’re familiar with functions in Python.
return Statement in Python
A function is capable of returning a value as a result of calling it.
This means we can create a function to, for example, take two or more arguments and add the entire arguments together and return the final result.
The returning part is done using the return statement.
Python return Statement Declaration
The return keyword in Python is used to create a statement in a function that will cause:
-
It will terminate the target function call.
-
It will return a value as a result to the caller.
This is how we create a return statement in Python functions:
return value
Where value in the statement above is the object that should be returned to the caller (the place where the call to the function occurred. (Note that objects will stay in the memory and only their pointer will return using the return statement! Basically, a copy of that object won’t return)
Also, remember that any statement after the return statement will be ignored because the return statement acts as the terminator of a function and so the execution engines won’t check other statements after a call to the return.
Example: using return statement in Python
def add(val1, val2):
return val1 + val2
res = add(20, 30)
print(res)
Output:
50
Here the, add function takes two arguments, add them together and returns the final value as the result of calling this function.
See that the call to the add() function occurred on the right side of the assignment = operator. After calling the add() function, the return value of this function will replace the call and so the res variable will take the return value of this function.
Multiple return Statement in Python
Within the body of a function we can set the return statement multiple times in multiple places! This is mainly happening when in that function there are multiple conditional statements (created via if statement for example) and so we can put a return statement within each of those conditional statements if needed.
Again, even though we can put multiple return statements within a function, only one call to this statement is needed to terminate the function and return the result.
Example: using multiple return statement in Python
In the example below we’re about to use if-elif-else statements. If you’re not familiar with these statements, it’s fine for now, skip this section and move on to other lessons. In the if-else section we will get back to this topic and you’ll learn about them.
def add(val1, val2):
if val1 >100:
return 100+val2
elif val2>300:
return 200+val1
else:
return 500
res = add(20, 30)
print(res)
Output:
500
Python Return Multiple Values
The return statement is capable of returning any type of object. This includes lists, tuple etc. where the object contains multiple values in its body.
Note: the list and tuple are explained in later sections.
Example: returning multiple values in Python
def listFunc():
li = [1,2,3,4,5,6]
return li
res = listFunc()
print(res)
Output:
[1, 2, 3, 4, 5, 6]