Online Tutorials & Training Materials | STechies.com
Register Login

What is the Use of Return Statement in Python?

03 Sep 2019 11:55 am || 0

In Python we use “return” as a keyword, here we can use a function with or without a return statement. If any function is called with return statement it simply returns the value and exit a function.

Return statement simply returns the values as output whereas print() function simply print the value.

Example With Return Statement

# Python 3 Code
# working on return statement
def addvalue(a, b):
 return a + b
c = addvalue(10, 34)
print(c)

Output

44

Explanation

Here we have created a function name addvalue() to add two value a & b and return the value as output by using the return statement. The def keyword is used for creating a method called addvalue(a,b) that takes two parameters and returns their summation. The result of a+b is provided to the user by the return statement.

Python Return Statement

Then the method addvalue() is called with arguments 10 and 34. The value of the method is stored in the variable c. The next statement prints out the value of c.

The final output of the addvalue() method is 44.

Example Without Return Statement

# Python 3 Code
# Function without return statement

def addvalue(a, b):
# Print the value of a+b
 print(a + b)

addvalue(10, 34)

Output

44

Explanation

In the above-given example, we have used print function to print the output of the function. As you can see in the above example print function returns nothing but print the value directly as output.

Python Without Return Statement

The addvalue() method defined by the def keyword takes two parameters. The parameters a and b are added using the print() function. The last line of the code invokes the addvalue() method. Two arguments 10 and 34 are passed to the addvalue() method. The final output is 44.

Python Return Multiple Values

A function can return only one value or object as output, but if you want to return multiple values then you can return the same with the help of list, dictionary and tuple.

All you need to do is convert your multiple outputs into a list (array), dictionary or tuple, and return them as a single object.

Example:

# Python 3 Code
# Function return multiple value as list

def myfunction(a, b):
   # Print the value of a+b
   add = a + b
   sub = a - b
   return(add, sub)

# Take multiple value in list
multiv = myfunction(10, 34)

# Print values in list
print('Addition: ', multiv[0]);
print('Subtraction: ' , multiv[1]);

Output:

Addition: 44
Subtraction: -24

Explanation

Here, a method called myfunction() is defined and has two parameters a and b. The two parameters are added/subtracted and stored in a variable called add/sub respectively. The return statement returns the value of add and sub.

Python Multiple Return Statement

Then the myfunction() method is called with the arguments 10 and 34. The result of the function call is stored in the variable multiv. So, now the multiv variable has two values – the result of (a+b) and (a-b). The first print() statement prints out the element at the first index of the variable multiv. Similarly, the next print statement prints out the element at the second index of the multiv variable.

Return True, False & String

Example:

# Python 3 Code
# Function return Boolean True/False

def myfunction(a, b):
    if(a > b):
        return True # Return True
    elif(a == b):
        return 'A is Equal to B' # Return String
    else:
        return False # Return False

# Check Boolean
print(myfunction(10, 34))
print(myfunction(10, 10))
print(myfunction(22, 11))

Output

False
A is Equal to B
True

Explanation

In this program, a function called myfunction() is defined. It takes two parameters a and b. An if statement checks whether the value of a is greater than b. If this is true, the return statement returns True. Else, if the value of a is equal to b, a string 'A is Equal to B' is returned by the function. Otherwise, False is returned by the program.

Python Boolean Return Statement

In the end, there are three print statements that display the result of calling the myfunction() method. As the function is passed two values 10 and 34, the first output is False, as 10 is lesser than 34. The next two arguments are 10 and 10. So the output is A is Equal to B.

The last two arguments passed to the myfunction() method are 22 and 11. Thus, as the 22 is greater than 11, the final output is True.

Difference between Return and Print Statement in Python

Return

Print()

Returns the value of a function as output.

Print the value of function on terminal.

The output of the function can be pass to other function.

Output can not pass to other function.

# Python 3 Code
# working on return statement

def addvalue(a, b):
 return a + b

c = addvalue(10, 34)
print(c)

 

def addvalue(a, b):
# Print the value of a+b

 print(a + b)

addvalue(10, 34)

 

Conclusion

The return and print statements are equally important for programming.

If you just want to execute a simple function that returns a value, the return statement will be enough. In case you want to return a value but also have to print it to the terminal, you need to use the print() method. The print() method can be easily used for displaying the result of a function call to the user.