Home »
Python
Python return Keyword
By IncludeHelp Last updated : December 07, 2024
Description and Usage
The return is a keyword (case-sensitive) in python, it is used to return a value (optional) to the calling function and transfers the control from called function to the calling function.
Syntax
Syntax of return keyword:
return [value]
Here, value is optional, if we do not want to return a value to the calling function, we can simply use a return to exit from the function.
Sample Input/Output
# function to return sum of two numbers
def sumof2no(x, y):
result = x+y
return result
print(sumof2no(10,20))
Output:
30
Example 1
Write a function to demonstrate example of return keyword.
# python code to demonstrate an example
# of return keyword
# a function
def myfunc():
print("myfunc()...")
print("first line...")
return
print("second line...")
# main code
# calling the function
myfunc()
Output
myfunc()...
first line...
Example 2
Write a function to add two numbers and return the sum
# python code to demonstrate an example
# of return keyword
# function to return sum of two numbers
def sumof2no(x, y):
result = x+y
return result
# main code
# calling the function
a = 100
b = 300
# printing sum
print("sum = ", sumof2no(a,b))
Output
sum = 400