Tuesday, July 2, 2019

Python return Statement With Examples

In this tutorial you’ll learn about return statement in Python which is used to exit a function and return a value.

Note that returning a return result from a function is not mandatory, return is not required if a function does not return a value. If no explicit return statement is used in a function, return None is implicitly returned by the function.

A return statement may or may not return a value. If a return statement is followed by a value (or expression) that value or the value after evaluating the expression is returned. If return statement without any value is used then the execution of the function terminates even if there are more statements after the return statement.

Python return statement examples

Let’s try to understand usage of return statement with some examples.

1- Return value from a function using return statement.

def sum(num1, num2):
    print('in sum function')
    return num1+num2

result = sum(16, 5)
print('sum is', result)

Output

in sum function
sum is 21

In the function return statement is followed by an arithmetic expression num1+num2, that expression is evaluated and the value is returned from the function.

2- A function with no return statement. You may have a function that doesn’t return any value in that case you need not write the return statement. If there is no return statement Python implicitly returns None.

def sum(num1, num2):
    print('in sum function')
    print('sum is', num1+num2)

sum(16, 5)

Output

in sum function
sum is 21

3- Using Python return statement with no return value. In the function an array is iterated to find a passed value, as soon as that value is found come out of the function using return.

from array import *
def find_in_array(arr, value):
    for i in arr:
        if i == value:
            print(value, 'found in array')
            #come out of function as value is found
            return
        
a = array('i', [3,4,5,6,7])
find_value = 5
find_in_array(a, find_value)

Output

5 found in array

4- If there are statements after return in a function those are not executed because the execution of the function is terminated when the return statement is encountered.

def sum(num1, num2):
    sum = num1+num2
    return sum
    #not reachable
    print('in sum function after calculation')

result = sum(16, 5)
print('sum is', result)

5- In Python, return statement can return multiple values. Multiple values can be returned as a tuple, list or dictionary. When multiple values are returned as comma separated values from a function, actually a tuple is returned.

def sum(num1, num2):
    sum = num1+num2
    # returning multiple values
    return sum,num1,num2 # can also be written as (sum, num1, num2)


result, num1, num2 = sum(16, 5)
print('sum of %d and %d is %d' % (num1, num2, result))

Output

sum of 16 and 5 is 21

That's all for this topic Python return Statement With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python assert Statement
  2. pass Statement in Python
  3. Polymorphism in Python
  4. Class And Object in Python
  5. Namespace And Variable Scope in Python

You may also like-

  1. Removing Spaces From String in Python
  2. Global Keyword in Python With Examples
  3. Variable Length Arguments (*args), Keyword Varargs (**kwargs) in Python
  4. List Comprehension in Python With Examples
  5. First Java Program - Hello World Java Program
  6. instanceof Operator in Java
  7. Array Rotation Java Program
  8. Benefits, Disadvantages And Limitations of Autowiring in Spring

No comments:

Post a Comment