Return Statement in Python

Return Statement in Python

In our function examples, we have not used the ‘return’ statement up to now. But you will be surprised to know that every function has an implicit return statement in the function body (after end of the function statements by default). This implicit return statement by default returns nothing, this means none. But you can change this default behavior by explicitly using return statement to return some value back.

हमारे फंक्शन उदाहरणों में, हमने अब तक फंक्शन ‘return’ स्टेटमेंट का उपयोग नहीं किया है। लेकिन आपको यह जानकर आश्चर्य होगा कि प्रत्येक फंक्शन का फंक्शन बॉडी में एक अंतर्निहित रिटर्न स्टेटमेंट होता है (डिफॉल्ट रूप से फंक्शन स्टेटमेंट के अंत के बाद)। डिफॉल्ट रिटर्न द्वारा यह निहित रिटर्न स्टेटमेंट कुछ भी नहीं है, इसका मतलब कोई नहीं है। लेकिन आप कुछ वैल्यू रिटर्न करने के लिए स्पष्ट रूप से रिटर्न स्टेटमेंट का उपयोग करके इस डिफॉल्ट व्यवहार को बदल सकते हैं।

In other words, function can take input values as parameters and executes business logic and returns output to the caller with return statement.

दूसरे शब्दों में, फंक्शन इनपुट मानों को पैरामीटर के रूप में ले सकता है और व्यापार तर्क को निष्पादित कर सकता है और रिटर्न स्टेटमेंट के साथ कॉलर को आउटपुट देता है।

Syntax: return <expression (s) >

Note:

  1. Every function in Python returns something. If the function doesn’t have any return Statement, then it returns none.

पायथन में प्रत्येक फंक्शन कुछ रिटर्न देता है। यदि फंक्शन में कोई रिटर्न स्टेटमेंट नहीं है, तो यहां कोई भी रिटर्न नहीं है।

  1. Once you return a value from a function, it immediately exits that function. Therefore, any code written after the return statement is never executed.

एक बार जब आप किसी फंक्शन से कोई वैल्यू रिटर्न करतें हैं, तो वह तुरंत उस फंक्शन से बाहर निकल जाता है। इसलिए, रिटर्न स्टेटमेंट के बाद लिखे गए किसी भी कोड को कभी भी निष्पादित नहीं किया जाता है।

  1. In other languages like C, C++ and Java, function can return only one value. But in Python, a function can return any number of values.

C, C++ और जावा जैसी अन्य भाषाओं में, फंक्शन केवल एक वैल्यू रिटर्न कर सकता है। लेकिन पायथन में, एक फंक्शन किसी भी संख्या का वैल्यू रिटर्न कर सकता है।

Example 1: write a function to accept two numbers as input and return sum.
इनपुट और रिटर्न सम के रूप में दो नंबरों को स्वीकार करने के लिए एक फंक्शन लिखें।

def add (a, b):
    return a+b
result = add (10,20)
print(" the sum of two values is : ", result) 
print(" the sum of two values ", add (100, 200))

Output:
the sum of two values is :  30
 the sum of two values  300

Example 2: if we are not writing return statement then default return value is none.
अगर हम रिटर्न स्टेटमेंट नहीं लिख रहे हैं तो डिफॉल्ट रिटर्न वैल्यू कोई नहीं है।

def add (a, b) :
    print ("without return statement")
result = add (10,20) 
print(" the sum of two values is : ", result) 
print(" the sum of two values is : ", add (100, 200))

Output:
without return statement
 the sum of two values is :  None
without return statement
 the sum of two values is :  None

Example 3: Returning multiple values from a function.
एक फंक्शन से कई वैल्यू रिटर्न कर रहा है।

def calc(a, b):
    c = a+b 
    d = a-b 
    e = a*b 
    f = a/b 
    return c, d, e, f
x = int (input ("Enter x value : ")) 
y = int (input ("Enter y value : "))
add, sub, mul, div = calc(x, y) 
print("Addition of two values :", add) 
print ("Subtraction of two values :", sub) 
print ("Multiplication of two values :", mul)
print ("Division of two values :", div)

Output:
Enter x value : 20
Enter y value : 10
Addition of two values : 30
Subtraction of two values : 10
Multiplication of two values : 200
Division of two values : 2.0

Example 4: You can present the above example in different way.
आप उपरोक्त उदाहरण को विभिन्न तरीके से दिखा सकते हैं।

def calc(a, b):
    return a+b,a-b,a*b,a/b
x = int (input ("Enter x value : ")) 
y = int (input ("Enter y value : "))
result = calc(x, y) 
for i in result:
    print (i)

Output:
Enter x value : 20
Enter y value : 10
30
10
200
2.0

Example 5: Let’s look at an example where we will return the Boolean value of the argument of a function. We will use bool( function to get the Boolean value of the object.
एक उदाहरण देखें जहां हम एक फंक्शन के तर्क का बूलियन वैल्यू रिटर्न करेंगे। हम bool() फंक्शन का उपयोग ऑब्जेक्ट के बूलियन वैल्यू को प्राप्त करने के लिए करेंगे।

def boolFun (x):
    return bool(x)
print ("Boolean value returned by bool Fun (False) is :",boolFun (False)) 
print ("Boolean value returned by bool Fun (True) is :", boolFun (True)) 
print ('Boolean value returned by bool Fun ("AA") is :', boolFun ("AA"))

Output:
Boolean value returned by bool Fun (False) is : False
Boolean value returned by bool Fun (True) is : True
Boolean value returned by bool Fun (“AA”) is : True

Example 6: Sometimes we want to convert a number of variables into a tuple. Let’s see how to write a function to return a tuple from a variable number of arguments.
कभी-कभी हम कई वेरिएबल्स को टपल में बदलना चाहते हैं। आइए देखें कि एक वैरियेबल नम्बर में तर्कों को वापस करने के लिए एक फंक्शन कैसे लिखें।

def TupleFun (*args):
    mlist = [] 
    for arg in args:
        mlist.append (arg * 10) 
    return tuple (mlist)
t = TupleFun (1, 2, 3)
print ("Tuple returned by TupleFun (1,2,3) is :", t) 

Output:
Tuple returned by TupleFun (1,2,3) is : (10, 20, 30)

Chapter wise Model Paper Link

About Me