Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
The return Statement in Python
The return statement in Python is used to send a value back from a function to its caller and exit the function. Since everything in Python is an object, the return value can be any object such as numeric types (int, float), collections (list, tuple, dict), or even other functions.
Key features of the return statement −
- It cannot be used outside a function.
- Any code written after a
returnstatement within the same block is dead code and will never be executed. - If no value is specified,
Noneis returned implicitly.
Syntax
def some_function(parameters):
return <expression>
Example
Following is a simple example of the return statement ?
def welcome(msg):
return msg + " from TutorialsPoint"
print(welcome("Good morning"))
The output of the above code is ?
Good morning from TutorialsPoint
Use of return Statement in Python
Functions can display results internally using print(), but it is best practice to return the result so the caller can decide what to do with it. The return statement terminates the executing function and passes control back to the caller.
Example
In this example, the function calculates the sum of the first 15 Fibonacci terms, prints it inside the function, and also returns it to show both approaches produce the same value ?
def sum_fibonacci(terms):
first_term = 0
second_term = 1
sum_series = 0
for i in range(0, terms):
sum_series = sum_series + first_term
next_term = first_term + second_term
first_term = second_term
second_term = next_term
print("Sum inside the function = {}".format(sum_series))
return sum_series
sum_result = sum_fibonacci(15)
print("Sum outside the function = {}".format(sum_result))
The output of the above code is ?
Sum inside the function = 986 Sum outside the function = 986
Returning a Function Using return Statement
Since functions in Python are first-class objects, a function can return another function. Functions that return or accept other functions are called higher-order functions. This pattern is commonly used to create closures ?
Example
def finding_sum(num1):
def add(num2):
return num1 + num2
return add
sum_func = finding_sum(5)
print("The sum of the two numbers is: {}".format(sum_func(10)))
The output of the above code is ?
The sum of the two numbers is: 15
Here, finding_sum(5) returns the inner add function with num1 bound to 5. Calling sum_func(10) then computes 5 + 10.
Returning None Using return Statement
Python functions always return a value. If return is omitted or used without an expression, the function returns None implicitly. Using return None explicitly is recommended when a function has multiple return paths, to make the termination points clear to other programmers ?
Example
def check_prime(numbers):
prime_list = []
for num in numbers:
if num
The output of the above code is ?
The prime numbers in the list are: None
Returning Multiple Values Using return Statement
The return statement can return multiple values separated by commas. Python packs them into a tuple automatically. This is useful when multiple calculations need to be performed on the same dataset ?
Example
import statistics as stat
def finding_stats(data):
return stat.mean(data), stat.median(data), stat.mode(data)
list_numbers = [5, 7, 13, 17, 17, 19, 33, 47, 83, 89]
print("The mean, median and mode of the data is: {}".format(finding_stats(list_numbers)))
The output of the above code is ?
The mean, median and mode of the data is: (33, 18.0, 17)
Conclusion
The return statement is essential for passing results out of functions in Python. It can return any object type, multiple values as a tuple, other functions, or None implicitly when no value is specified.
