Home »
Python
return keyword with example in Python
Python return keyword: Here, we are going to learn about the return keyword with example.
Submitted by IncludeHelp, on April 15, 2019
Python return keyword
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 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.
Example:
# function to return sum of two numbers
def sumof2no(x, y):
result = x+y
return result
print(sumof2no(10,20))
Output:
30
Python examples of return keyword
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