How to return value from user-defined functions in Python
In this tutorial, we will discuss how to return a value from a user-defined function in Python.
The return value of the user-defined function
We have two types of functions: void functions and functions with a return value. A void function has no return statement and hence returns None whereas the function with a return value returns the result of the function. The return statement is written using the keyword ‘return’ followed by the return expression. The syntax for the function with return value:-
def FunctionName (argument1, argument2, …) :
As we define a function and assign a return value to it and then call that function, then that function call is superseded by the return value of that function. Further by assigning this function call to a variable, we can store the return value in that particular variable. We can also call this function inside a print function to directly print the return value without using an extra variable.
Below are some examples showing the use of the return statement using different functions.
The return value of a void function is by default ‘None’. For example.
def func(): print("hello") func()
This function has only a print statement and no return statement. To check the return type of this function we can call this function within another print statement as follows.
Functions with return value:
An example of a function with a return value is shown below.
def add(x,y): z= x+y return z r=add(2,3) #assigning the function call to a variable print(r)
In the above code, we defined a function ‘add()’ with two parameters’ x’ and ‘y’. Inside this function, we added the two parameters and assigned the sum to a variable ‘z’ which is returned using a return statement. We then called this function and assigned it to a new variable ‘r’. This assigned the return value of the function to the variable ‘r’. And finally, we printed the value of r.
A return statement in Python can return any type of data like integer, float, list, tuple, etc.
def func(x): z= [1,2,3,4,5] z.append(x) return z r=func(23) #assigning the function call to a variable print(r)
[1, 2, 3, 4, 5, 23]
The above code is printing a list of integers as a return value for the function, ‘func()’ that appends a value(‘x’, taken as a parameter) to an already defined list.
Python has the feature of returning multiple values together using a tuple.
def func(x,y): a=x+5 b=y+5 return a,b r=func(2,3) #assigning the function call to a variable print(r)
In the above code, the ‘return’ statement outputs two values, a and b in the form of a tuple.
Note: If we have a function with a return statement but without any return expression, then the return value for that function will be None.
def func(x,y): z=x+y return print(func(1,2))
The ‘return’ statement in the above code is used only for terminating the function execution.
The data type for the return statement
The data type of the return statement of a function can be denoted by using function annotations. It is optional and is only for user reference hence it does not attach any significance to the Python interpreter. For example
def func(x: int) -> int: print(x)
Here in the above code, a function ‘func’ has defined a parameter ‘x’ that returns an integer value as specified by the ‘int’ annotation followed by ‘->’ symbol. This declaration of the return type makes the code more readable.
Leave a Reply