Return value in function means that you can return values using the return statement. When a function is called and it has a return statement, it immediately exits the function and sends a value (or None if there is no explicit return value) back to the caller.
Syntax for Return value in function
def function_name():
# Some code here
return value # This is the value to be returned
result = function_name() # Calling the function and storing its return value in the variable ‘result’
Example 1
Add two numbers and return the total
def sum(x,y):
tot=x+y
return tot
total=sum(10,20)
avg=total/2
print(avg)
Explanation
- Here tot is created and must be passed outside the function by giving “return tot”
- total = sum(10,20) means calling the function and reassigning the result to the “total” variable.
- But if you didn’t write return tot, you wouldn’t return the result to the “total” variable.
- Since you return the tot outside, you can use that total further in the main function. That’s why you can write avg=total/2
Output
15.0
Note
The output should not be 15. It's 15.0 because the variable "total" became a float because of the division.
Example 2
To get the maximum of two numbers using a function
def max(x,y):
if x>y:
max=x
else:
max=y
return max
print(max(10,20))
Explanation
- Here “max” is retuned outside of the function and passed it to the print function.
- So that the output has sent to the screen by providing print(max(10,20))
Output
20
Example 3
Above Example 2, is changed to input 2 numbers by keyboard
def max(x,y):
if x>y:
max=x
else:
max=y
return max
n1=int(input("Enter Number 1 :"))
n2=int(input("Enter Number 2 :"))
print(max(n1,n2))
Explanation
- Here
- Enter Number 1 : and Enter Number 2 : will be appeared on the screen
- If the user input 45 and 22,
Output
45
Return multiple values from a function
How to return multiple values from a function in Python?
In Python user defined functions, you can return multiple values by simply separating them with commas in the return statement.
Unpack the multiple return values
You can unpack multiple return values and assign them to separate variables.
Example
def cal(n1,n2):
tot=n1+n2
avg=tot/2
if n1>n2:
max=n1
else:
max=n2
return tot, avg, max
total, average, maximum=cal(50,60)
print(total,average,maximum)
Explanation
- Here
- Enter Number 1 : and Enter Number 2 : will be appeared on the screen
- If the user input 45 and 22,
Output
45
Output
110 55.0 60
but if we call the function as follows,
total, average, maximum=cal(50,60)
You can unpack the multiple return values and assign them to separate variables as above.
Advantages of Return value in Functions
- By returning data or results to the caller, this facilitates data transfer between different parts of your program.
- By returning values, functions become more versatile and can adapt to different contexts.
- Returning values promotes separate program functions into independent pieces. Each with a specific responsibility, and pass data between them using return values.