×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.