How does return() in Python work?
In order to get a value from a function in any programming language, we use the return() statement. Likewise, in Python, the return() statement is used to exit a function and returns a value from a function. In this tutorial, we will read about various ways to use the return() statements in Python.
Table of contents
- Introduction to return() statement
- Returning Multiple Values in Python
- Argument in return() function
- Function returning another function
- Closing thoughts
return() in Python
The return() statement, like in other programming languages ends the function call and returns the result to the caller. It is a key component in any function or method in a code which includes the return keyword and the value that is to be returned after that.
Some points to remember while using return():
- The statements after the return() statement are not executed.
- return() statement can not be used outside the function.
- If the return() statement is without any expression, then the NONE value is returned.
Syntax of return() in Python:
def func_name(): statements.... return [expression]
Using a return() statement for returning multiple values in Python
Python also gives an option to return multiple values from a function and in order to do that the user just needs to add multiple return values separated by commas. Also known as a tuple, can be created with or without using the ().
def statFun(a, b): difference = a-b percent_diff = (difference/a)*100 return difference, percent_diff; difference, percent_diff = statFun() print (difference) print (percent_diff)
Here, the function statFun() gives to values and by using tuple we return both of the values.
return() in Python with an argument
In Python, arguments can be used with a return statement. To begin with, arguments are the parameter given by the user and as we know, the argument is the value(s) as input, which is given by the user to the function.
def divNum(a, b): if b != 0 return a/b; else: return 0; print (divNum(4, 2)) print (divNum(2, 0))
Here, the function divNum() accepts two arguments and if the second argument is non zero it divides them otherwise returns 0.
Function returning another function in Python
As we know functions are treated as first-class objects in Python, therefore we can return a function from another function. A first-class object is an object that can be assigned to a variable, passed as an argument to a function, or used as a return value in a function.
A function that takes a function as an argument, returns a function as a result, or both is known as a higher-order function.
def func_1(a): def func_2(b): return a-b return func_2 x = func_1(100) print ("The value of a-b is", x(50)) def another_func(a): return a*10 def func(): return another_func y = func() print ("\nThe value of a*b is" y(10))
The value of a-b is 50 The value of a*b is 100
The return statement sends any object from the function back to the caller code. Since the return statement is a key part of any function or method, if you learn how to use it correctly, you can move to complex codes. One can learn about more Python concepts here.