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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.