Functions in Python: def Keyword, Function Call, Return Value Notes in Hindi & English : फंक्शन्स: def कीवर्ड, पैरामीटर्स और return स्टेटमेंट और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Functions in Python: def Keyword, Function Call, Return Value (फंक्शन्स: def कीवर्ड, पैरामीटर्स और return स्टेटमेंट) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: In Python, functions are reusable first-class objects defined at runtime using the def keyword, taking optional parameters and returning results via return. If execution reaches the end of a function without a return value, Python implicitly returns None, while comma-separated return values pack automatically into a tuple.

अध्याय सार: पायथन में फंक्शन्स पुन: उपयोग योग्य फर्स्ट-क्लास ऑब्जेक्ट्स हैं जिन्हें रनटाइम पर def कीवर्ड से परिभाषित किया जाता है और जो पैरामीटर्स लेकर return के जरिए परिणाम देते हैं। यदि फंक्शन बिना return मान दिए समाप्त हो जाए, तो पायथन स्वतः None लौटाता है, जबकि अल्पविराम से अलग कई मान return करने पर वे स्वतः एक टपल (tuple) बन जाते हैं।

Functions in Python: def Keyword, Function Call, Return Value: Detailed Notes & Architecture (फंक्शन्स: def कीवर्ड, पैरामीटर्स और return स्टेटमेंट)

1. The def Statement and First-Class Function Objects (def स्टेटमेंट और फर्स्ट-क्लास फंक्शन ऑब्जेक्ट्स)

[EN] Unlike C declarations processed purely at compile time, def in Python is an executable statement that creates a function object (types.FunctionType) when reached at runtime and binds it to the function name. The header consists of def, a valid identifier name, a parenthesized parameter list, and a colon, followed by an indented body whose first string literal automatically populates the __doc__ docstring attribute. Because functions are first-class objects, you can assign a function to another variable, store it in lists or dictionaries, pass it as an argument, or return it from another function. Referencing the name alone (my_func) accesses the function object itself, whereas appending parentheses (my_func()) invokes it.

[HI] C की तरह केवल कंपाइल-टाइम घोषणा के विपरीत, पायथन में def एक निष्पादन योग्य (executable) स्टेटमेंट है जो रनटाइम पर एक फंक्शन ऑब्जेक्ट बनाता है और उसे नाम से जोड़ता है। हेडर में def, मान्य नाम, कोष्ठक में पैरामीटर सूची और कोलन होता है, तथा बॉडी का पहला स्ट्रिंग लिटरल स्वतः __doc__ डॉकस्ट्रिंग एट्रिब्यूट बन जाता है। फंक्शन्स फर्स्ट-क्लास ऑब्जेक्ट होने के कारण इन्हें किसी वेरिएबल में सहेजा जा सकता है, लिस्ट/डिक्शनरी में रखा जा सकता है, या दूसरे फंक्शन में पास किया जा सकता है। केवल नाम (my_func) लिखने पर फंक्शन ऑब्जेक्ट मिलता है, जबकि कोष्ठक (my_func()) लगाने पर वह कॉल होता है।

2. Function Call Mechanics and Pass-by-Object-Reference (फंक्शन कॉल प्रक्रिया और पास-बाय-ऑब्जेक्ट-रेफरेंस)

[EN] Calling a function allocates a new stack frame with a local namespace mapping parameter names to the passed argument objects. Python uses pass-by-object-reference (also called call-by-sharing): arguments pass references to existing objects rather than copying values or exposing caller variable pointers. Consequently, mutating a mutable argument in place (such as lst.append(4)) modifies the caller's list, whereas rebinding the parameter (lst = [9]) affects only the local variable inside the function. Name lookups inside a function follow the LEGB resolution order: Local, Enclosing, Global, and Built-in scopes.

[HI] फंक्शन कॉल होने पर एक नया स्टैक फ्रेम और लोकल नेमस्पेस बनता है जहाँ पैरामीटर नाम पास किए गए ऑब्जेक्ट्स से जुड़ते हैं। पायथन 'पास-बाय-ऑब्जेक्ट-रेफरेंस' (कॉल-बाय-शेयरिंग) अपनाता है, जिसमें ऑब्जेक्ट की प्रति बनाने के बजाय उसका रेफरेंस साझा किया जाता है। इसलिए किसी म्यूटेबल आर्गुमेंट में बदलाव (जैसे lst.append(4)) कॉलर की लिस्ट को बदल देता है, पर पैरामीटर को नया मान देने (lst = [9]) का असर केवल लोकल स्कोप तक रहता है। फंक्शन के भीतर वेरिएबल खोजने में LEGB (Local, Enclosing, Global, Built-in) क्रम का पालन होता है।

3. The return Statement, Implicit None, and Multiple Values (return स्टेटमेंट, अंतर्निहित None और एकाधिक मान)

[EN] The return statement immediately exits the current function and sends the evaluated expression back to the caller; any statements placed after an executed return inside that branch are never reached. If a function contains no return statement, executes a bare return without an expression, or finishes without hitting any return path, Python implicitly returns None. Python syntax allows returning comma-separated values such as return quotient, remainder, which implicitly packs both values into a single tuple (quotient, remainder). Callers can receive that returned tuple directly or unpack it cleanly via q, r = divide(17, 5).

[HI] return स्टेटमेंट फंक्शन को तुरंत समाप्त कर देता है और मूल्यांकित मान को कॉलर तक वापस भेजता है; उसी शाखा में return के बाद लिखा कोड कभी नहीं चलता। यदि फंक्शन में return न हो, खाली return लिखा हो, या कोई return पथ न चले, तो पायथन स्वतः None लौटाता है। return q, r की तरह अल्पविराम से अलग एकाधिक मान लौटाने पर पायथन उन्हें स्वतः एक टपल (q, r) में पैक करके लौटाता है। कॉलर उस टपल को सीधे ले सकता है या q, r = divide(17, 5) के रूप में अनपैक कर सकता है।

4. Parameters vs Arguments, Annotations, and Scope Rules (पैरामीटर बनाम आर्गुमेंट, एनोटेशन और स्कोप नियम)

[EN] By convention, parameters are the named placeholders in the def header, while arguments are the concrete values supplied during a function call. Variables assigned inside a function become local by default; reading a global variable works automatically, but modifying a global binding inside a function requires the global keyword (or nonlocal for enclosing scopes). Since Python 3.0 (PEP 3107), function definitions support optional type annotations such as def add(a: int, b: int) -> int:, stored in __annotations__ without runtime type enforcement. Defining one def inside another creates nested functions and closures capable of remembering enclosing variables even after the outer function returns.

[HI] तकनीकी रूप से, def हेडर में लिखे नाम 'पैरामीटर' कहलाते हैं और कॉल करते समय दिए गए वास्तविक मान 'आर्गुमेंट' कहलाते हैं। फंक्शन के अंदर असाइन किए गए वेरिएबल डिफ़ॉल्ट रूप से लोकल होते हैं; ग्लोबल वेरिएबल को पढ़ा जा सकता है, पर उसे री-असाइन करने के लिए global (या बाहरी फंक्शन के लिए nonlocal) कीवर्ड आवश्यक है। पायथन 3.0 से def add(a: int, b: int) -> int: जैसे टाइप एनोटेशन उपलब्ध हैं जो __annotations__ में स्टोर होते हैं पर रनटाइम टाइप जांच लागू नहीं करते। एक def के अंदर दूसरा def बनाने पर नेस्टेड फंक्शन और क्लोज़र (closure) बनते हैं।

Syntax, Formula & Technical Example (Functions in Python: def Keyword, Function Call, Return Value)

def stats(nums):
    """Return min and max as a tuple."""
    if not nums:
        return None
    return min(nums), max(nums)

low, high = stats([4, 9, 2])
print(low, high, type(stats([4, 9, 2])))
# Output: 2 9 <class 'tuple'>

Functions in Python: def Keyword, Function Call, Return Value Quick Reference & Comparison Table

Function FeaturePython BehaviorKey Exam Fact
def headerCreates FunctionType object at runtimeBound to identifier; can even be inside if/else
No return / bare returnImplicitly evaluates to Nonediffers from C void functions which yield no value
return a, bReturns a single packed tuple (a, b)Supports tuple unpacking on assignment
LEGB lookupLocal -> Enclosing -> Global -> Built-inAssigning a name makes it Local unless global/nonlocal used

Functions in Python: def Keyword, Function Call, Return Value Top 20 Bilingual MCQs with Answers

Q1. What value does a Python function return if it has no `return` statement?

(यदि किसी पायथन फंक्शन में `return` स्टेटमेंट न हो, तो वह क्या लौटाता है?)

(A) 0

(B) Raises ValueError

(C) None

(D) False

Correct Answer: (C)

[EN] Every Python function returns None implicitly if control reaches the end without an explicit return value.

[HI] जब फंक्शन बिना किसी स्पष्ट return मान के समाप्त होता है, तो वह स्वतः None लौटाता है।

Q2. What data type does `def f(): return 10, 20` actually return?

(`def f(): return 10, 20` वास्तव में किस डेटा टाइप का मान लौटाता है?)

(A) dict

(B) set

(C) list

(D) tuple

Correct Answer: (D)

[EN] Comma-separated expressions in a return statement are automatically packed into a tuple (10, 20).

[HI] return स्टेटमेंट में अल्पविराम से अलग मान स्वतः एक tuple (10, 20) के रूप में लौटते हैं।

Q3. What is the output of: def append_item(lst): lst.append(99) lst = [0] a = [1, 2] append_item(a) print(a)

(निम्न कोड का आउटपुट क्या होगा? def append_item(lst): lst.append(99) lst = [0] a = [1, 2] append_item(a) print(a))

(A) [1, 2]

(B) [0]

(C) [1, 2, 99, 0]

(D) [1, 2, 99]

Correct Answer: (D)

[EN] lst.append(99) mutates the shared list object, whereas lst = [0] only rebinds the local parameter name.

[HI] lst.append(99) साझा की गई लिस्ट में बदलाव करता है, जबकि lst = [0] केवल लोकल पैरामीटर का रेफरेंस बदलता है।

Q4. How does Python access a function's docstring programmatically?

(पायथन में किसी फंक्शन की डॉकस्ट्रिंग को प्रोग्राम द्वारा कैसे पढ़ा जाता है?)

(A) func.docstring()

(B) func.__comment__

(C) func.__info__

(D) func.__doc__

Correct Answer: (D)

[EN] The first string literal inside a function body is stored in the function's __doc__ attribute.

[HI] फंक्शन बॉडी का पहला स्ट्रिंग लिटरल फंक्शन के __doc__ एट्रिब्यूट में संग्रहीत होता है।

Q5. What does `print(type(func))` output if `def func(): pass` is defined?

(`def func(): pass` परिभाषित होने पर `print(type(func))` क्या आउटपुट देगा?)

(A) <class 'NoneType'>

(B) <class 'method'>

(C) <class 'def'>

(D) <class 'function'>

Correct Answer: (D)

[EN] A function created with def is an instance of Python's function class (<class 'function'>).

[HI] def से बना फंक्शन पायथन की function क्लास (<class 'function'>) का एक ऑब्जेक्ट होता है।

Q6. What is the output of: def greet(): print('Hi') res = greet() print(res)

(निम्न कोड का आउटपुट क्या होगा? def greet(): print('Hi') res = greet() print(res))

(A) Hi only

(B) Hi followed on next line by None

(C) Hi followed by Hi

(D) None only

Correct Answer: (B)

[EN] greet() prints 'Hi' and returns None implicitly, so print(res) outputs None.

[HI] greet() पहले 'Hi' प्रिंट करता है और अंतर्निहित रूप से None लौटाता है, इसलिए res का मान None प्रिंट होता है।

Q7. Which scope resolution order does Python follow inside functions?

(पायथन में फंक्शन्स के भीतर वेरिएबल खोजने का क्रम क्या होता है?)

(A) LGEB (Local, Global, Enclosing, Built-in)

(B) GBEL (Global, Built-in, Enclosing, Local)

(C) ELGB (Enclosing, Local, Global, Built-in)

(D) LEGB (Local, Enclosing, Global, Built-in)

Correct Answer: (D)

[EN] Python resolves identifiers from innermost Local scope outward through Enclosing, Global, and Built-in.

[HI] पायथन नामों को सबसे भीतरी Local से शुरू कर क्रमशः Enclosing, Global और Built-in स्कोप में खोजता है।

Q8. What happens when you write `x = 10` globally and `def f(): x += 1` without `global x`, then call `f()`?

(ग्लोबल में `x = 10` होने पर बिना `global x` के `def f(): x += 1` चलाकर `f()` कॉल करने पर क्या होगा?)

(A) SyntaxError

(B) x becomes 11 globally

(C) UnboundLocalError

(D) f() returns 11 quietly

Correct Answer: (C)

[EN] Assigning to x inside f makes x local across the whole function, so reading x before assignment raises UnboundLocalError.

[HI] फंक्शन में x पर असाइनमेंट करने से x लोकल बन जाता है, अतः बिना आरंभिक मान के x+=1 चलाने पर UnboundLocalError आता है।

Q9. What is the difference between `f = my_func` and `f = my_func()`?

(`f = my_func` और `f = my_func()` में क्या अंतर है?)

(A) The second assigns the docstring

(B) The first raises SyntaxError

(C) Both call the function identically

(D) The first assigns the function object itself; the second calls it and assigns its return value

Correct Answer: (D)

[EN] Parentheses trigger function invocation; omitting them passes or binds the function reference itself.

[HI] कोष्ठक लगाने पर फंक्शन कॉल होकर उसका रिटर्न मान मिलता है, जबकि बिना कोष्ठक के स्वयं फंक्शन ऑब्जेक्ट असाइन होता है।

Q10. What does this code print? def test(x): return x * 2 return x * 3 print(test(4))

(निम्न कोड का आउटपुट क्या होगा? def test(x): return x * 2 return x * 3 print(test(4)))

(A) 12

(B) 8

(C) 8 and 12

(D) SyntaxError

Correct Answer: (B)

[EN] The first return statement immediately exits test() with value 8; the second return is unreachable.

[HI] पहला return स्टेटमेंट 8 लौटाकर तुरंत फंक्शन समाप्त कर देता है; दूसरा return कभी नहीं चलता।

Q11. When does Python evaluate the `def` statement itself?

(पायथन `def` स्टेटमेंट को कब निष्पादित करता है?)

(A) Only when the function is called for the first time

(B) Never; def is purely a compiler hint

(C) Only during syntax lexing before any code runs

(D) At runtime when execution reaches the def statement

Correct Answer: (D)

[EN] def is an executable statement at runtime; that is why functions can even be defined conditionally inside if/else.

[HI] def रनटाइम पर निष्पादित होने वाला स्टेटमेंट है; यही कारण है कि if/else के भीतर भी फंक्शन परिभाषित किए जा सकते हैं।

Q12. Which keyword allows modifying a variable from an outer enclosing function (not module global) inside a nested function?

(नेस्टेड फंक्शन के भीतर बाहरी एनक्लोज़िंग फंक्शन के वेरिएबल को बदलने के लिए किस कीवर्ड का उपयोग होता है?)

(A) global

(B) extern

(C) nonlocal

(D) outer

Correct Answer: (C)

[EN] nonlocal binds an identifier to the nearest enclosing function scope rather than the module global scope.

[HI] nonlocal किसी वेरिएबल को मॉड्यूल ग्लोबल के बजाय निकटतम बाहरी फंक्शन के स्कोप से जोड़ता है।

Q13. Do Python type annotations like `def add(a: int, b: int) -> int:` prevent calling `add('x', 'y')` at runtime?

(क्या `def add(a: int, b: int) -> int:` जैसे टाइप एनोटेशन रनटाइम पर `add('x', 'y')` कॉल होने से रोकते हैं?)

(A) Only in Python 3.12+

(B) Yes; SyntaxError is raised

(C) Yes; TypeError is raised automatically

(D) No; annotations are metadata only and not enforced at runtime

Correct Answer: (D)

[EN] Python remains dynamically typed; annotations populate __annotations__ for tools like mypy and IDEs.

[HI] पायथन डायनामिकली टाइप्ड रहता है; एनोटेशन केवल मेटाडेटा हैं जिन्हें mypy जैसे टूल्स उपयोग करते हैं, रनटाइम इन्हें लागू नहीं करता।

Q14. What happens if you place `return` outside any function in Python?

(पायथन में किसी फंक्शन के बाहर `return` स्टेटमेंट लिखने पर क्या होता है?)

(A) SyntaxError: 'return' outside function

(B) It exits the Python script cleanly

(C) RuntimeError

(D) It returns None to the OS

Correct Answer: (A)

[EN] return is valid exclusively inside a def function body; using it at module or class top-level raises SyntaxError.

[HI] return केवल def फंक्शन के अंदर ही मान्य है; बाहर लिखने पर SyntaxError आता है।

Q15. Which terminology pair correctly describes `x` in `def square(x):` vs `5` in `square(5)`?

(`def square(x):` में `x` और `square(5)` में `5` के लिए सही शब्दावली क्या है?)

(A) x is a parameter; 5 is an argument

(B) x is an argument; 5 is a parameter

(C) x is a return slot; 5 is a keyword

(D) Both x and 5 are literals

Correct Answer: (A)

[EN] Parameters are variables listed in the function definition; arguments are actual values passed in the call.

[HI] फंक्शन परिभाषा में लिखा वेरिएबल पैरामीटर होता है, और कॉल करते समय भेजा गया वास्तविक मान आर्गुमेंट कहलाता है।

Q16. Which of the following statements about Python functions is NOT true?

(पायथन फंक्शन्स के बारे में निम्नलिखित में से कौन-सा कथन असत्य (NOT true) है?)

(A) return a, b packs the values into a tuple

(B) A function with a bare `return` statement raises SyntaxError

(C) Functions are first-class objects that can be passed to other functions

(D) A function without return evaluates to None

Correct Answer: (B)

[EN] A bare `return` statement (with no expression) is completely valid and immediately returns None.

[HI] बिना किसी मान के अकेला `return` लिखना पूरी तरह वैध है और यह तुरंत None लौटाता है।

Q17. Within Functions in Python: def Keyword, Function Call, Return Value, which function feature directly maps to: "Creates FunctionType object at runtime"?

(Functions in Python: def Keyword, Function Call, Return Value में कौन सा Function Feature सीधे "Creates FunctionType object at runtime" से संबंधित है?)

(A) LEGB lookup

(B) return a, b

(C) def header

(D) No return / bare return

Correct Answer: (C)

[EN] In Functions in Python: def Keyword, Function Call, Return Value, 'def header' handles 'Creates FunctionType object at runtime' (Bound to identifier; can even be inside if/else).

[HI] Functions in Python: def Keyword, Function Call, Return Value में 'def header' का कार्य/विवरण 'Creates FunctionType object at runtime' (Bound to identifier; can even be inside if/else) है।

Q18. Which option accurately describes 'No return / bare return' in Functions in Python: def Keyword, Function Call, Return Value?

(Functions in Python: def Keyword, Function Call, Return Value के संदर्भ में 'No return / bare return' की सही विशेषता कौन सी है?)

(A) Local -> Enclosing -> Global -> Built-in

(B) Implicitly evaluates to None

(C) Creates FunctionType object at runtime

(D) Returns a single packed tuple (a, b)

Correct Answer: (B)

[EN] 'No return / bare return' provides 'Implicitly evaluates to None' — key point: differs from C void functions which yield no value.

[HI] 'No return / bare return' का मुख्य विवरण 'Implicitly evaluates to None' है (differs from C void functions which yield no value)।

Q19. In Functions in Python: def Keyword, Function Call, Return Value, identify the correct parameter for the specification: "Returns a single packed tuple (a, b)".

(Functions in Python: def Keyword, Function Call, Return Value में "Returns a single packed tuple (a, b)" विवरण किस विकल्प के लिए सही है?)

(A) def header

(B) return a, b

(C) LEGB lookup

(D) No return / bare return

Correct Answer: (B)

[EN] 'return a, b' corresponds to 'Returns a single packed tuple (a, b)' (Supports tuple unpacking on assignment).

[HI] 'return a, b' का संबंध 'Returns a single packed tuple (a, b)' (Supports tuple unpacking on assignment) से है।

Q20. Considering Functions in Python: def Keyword, Function Call, Return Value, what key behavior defines 'LEGB lookup'?

(Functions in Python: def Keyword, Function Call, Return Value में 'LEGB lookup' की मुख्य भूमिका या मान क्या है?)

(A) Creates FunctionType object at runtime

(B) Implicitly evaluates to None

(C) Local -> Enclosing -> Global -> Built-in

(D) Returns a single packed tuple (a, b)

Correct Answer: (C)

[EN] 'LEGB lookup' represents 'Local -> Enclosing -> Global -> Built-in' (Assigning a name makes it Local unless global/nonlocal used).

[HI] 'LEGB lookup' का अर्थ/मान 'Local -> Enclosing -> Global -> Built-in' है (Assigning a name makes it Local unless global/nonlocal used)।

Functions in Python: def Keyword, Function Call, Return Value — Exam FAQs

Q1: Does Python use pass-by-value or pass-by-reference for function arguments?

क्या पायथन फंक्शन आर्गुमेंट्स के लिए पास-बाय-वैल्यू या पास-बाय-रेफरेंस का उपयोग करता है?

[EN] Python uses pass-by-object-reference (call-by-sharing). The object reference is passed by value: mutating a mutable object in place affects the caller, while reassigning the parameter variable does not.

[HI] पायथन पास-बाय-ऑब्जेक्ट-रेफरेंस (कॉल-बाय-शेयरिंग) का उपयोग करता है। इसमें ऑब्जेक्ट का रेफरेंस भेजा जाता है: म्यूटेबल ऑब्जेक्ट में किया गया बदलाव कॉलर को दिखता है, पर वेरिएबल को नया मान देने का असर लोकल रहता है।

Q2: Can a Python function call itself recursively?

क्या पायथन फंक्शन स्वयं को रिकर्सिव रूप से कॉल कर सकता है?

[EN] Yes, functions support recursion up to CPython's default recursion limit (sys.getrecursionlimit(), typically 1000 frames), beyond which RecursionError is raised.

[HI] हाँ, फंक्शन रिकर्शन का समर्थन करते हैं, जो CPython की डिफ़ॉल्ट सीमा (आमतौर पर 1000 फ्रेम्स) तक चलता है और उससे अधिक होने पर RecursionError देता है।

Q3: Can a function be defined inside an `if` block in Python?

क्या पायथन में `if` ब्लॉक के अंदर कोई फंक्शन परिभाषित किया जा सकता है?

[EN] Yes, because `def` is an executable runtime statement, you can conditionally create different implementations of a function inside `if/else` blocks.

[HI] हाँ, चूँकि `def` एक रनटाइम स्टेटमेंट है, इसलिए `if/else` शर्तों के आधार पर अलग-अलग फंक्शन परिभाषित किए जा सकते हैं।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)