To return a value from a function, use the Python return statement. In a function, the user can only utilize the return statement. It can only be used within the Python function. A return statement contains the return keyword as well as the value to be returned after that.
We can return a single value or multiple values using the return statement in the python function.
Returning Single Value using return Statement in Python Function
Approach:
- Create a function say multiply() which accepts two numbers as an argument
- Inside the function, multiply the above passed two numbers and store it in a variable
- Return the above multiplication result using the return statement
- Call the above-created multiply() function by passing some random two numbers as arguments
and store it in another variable - Here it multiplies the two numbers(3*4) that we pass by calling the function and stores the result
- Print the multiplication of the given two numbers.
- The Exit of the Program.
Below is the implementation:
# Create a function say multiply() which accepts two numbers as an argument def multiply(x, y): # Inside the function, multiply the above passed two numbers and # store it in a variable mul = x * y # Return the above multiplication result using the return statement return mul # Call the above created multiply() function by passing some random two numbers as arguments # and store it in another variable # Here it multiplies the two numbers(3*4) that we pass by calling the function and stores the result rslt = multiply(3, 4) # Print the multiplication of given two numbers print(rslt)
Output:
12
Returning Multiple Values using return Statement in Python Function
Approach:
- Create a function say multiply() which accepts two numbers as an argument
- Inside the function, multiply the above passed two numbers and store it in a variable
- Calculate the average of the given two numbers and store it in another variable
- Return the above multiplication, and average results using the return statement
- Here we can observe that we are returning multiple values
- Call the above-created multiply() function by passing some random two numbers as arguments
and store it in another variable - Here it stores both multiplication and average results of the given two numbers
- Print the multiplication and average of given two numbers.
- The Exit of the Program.
Below is the implementation:
# Create a function say multiply() which accepts two numbers as an argument def multiply(x, y): # Inside the function, multiply the above passed two numbers and # store it in a variable mul = x * y # Calculate the average of given two numbers and store it in another variable average = (x+y)/2 # Return the above multiplication, average results using the return statement # Here we can observe that we are returning multiple values return mul, average # Call the above created multiply() function by passing some random two numbers as arguments # and store it in another variable # Here it stores both multiplication, average results of the given two numbers rslt = multiply(3, 4) # Print the multiplication, average of given two numbers print("The multiplication, average of given two numbers is:") print(rslt)
Output:
The multiplication, average of given two numbers is: (12, 3.5)
NOTE:
Not only two values, but you can also return multiple values using the return statement in python function