Functions: Return, Scope और Flow
Values return करना
return statement caller को value वापस भेजता है और function तुरंत ख़त्म कर देता है। Function एक value, कई values (tuple के रूप में) या कुछ नहीं return कर सकता है।
def greet(name): # void function: no return
print('Hello', name)
def square(n): # returns one value
return n * n
def min_max(seq): # returns many values as a tuple
return min(seq), max(seq)
r = greet('Asha')
print(r, square(9), min_max([7, 2, 9]))
low, high = min_max([7, 2, 9])
print(low, high)def check(n):
if n % 2 == 0:
return 'even'
print('never printed') # unreachable after return
return 'odd'
print(check(4), check(7))Void functions और None
बिना return (या ख़ाली return) वाला function void function कहलाता है; यह None return करता है। Board questions में अक्सर ऐसे function का result print होता है — answer में None आता है।
Flow of execution
Python ऊपर से नीचे चलता है। def सिर्फ़ function define करता है। Call आने पर control function में कूदता है, body चलाता है, और call के बाद वाली line पर लौटता है।
def add(a, b): # line 1
c = a + b # line 2
return c # line 3
def show(): # line 5
print('Start') # line 6
s = add(3, 4) # line 7
print('Sum', s) # line 8
show() # line 10
print('End') # line 11Variables का scope (LEGB)
x = 'global'
def outer():
x = 'enclosing'
def inner():
print(x) # not local -> enclosing found first
inner()
outer()
print(x)global keyword
Function के अंदर global variable पढ़ना allowed है। उसे assign करने के लिए global declare करें।
count = 10
def double():
global count # refer to the global variable
count = count * 2
double()
double()
print(count)UnboundLocalError
अगर function अपनी body में कहीं भी किसी नाम को assign करता है, तो वह नाम पूरे function में local है। Assignment से पहले उसे पढ़ना error है:
c = 10
def g():
print(c) # c is local here because of the next line
c = 5
g()UnboundLocalError: cannot access local variable 'c' where it is not associated with a value
(पुराने Python versions कहते हैं: local variable 'c' referenced before assignment।) Fix: global c या कोई दूसरा नाम।
Mutable और immutable objects भेजना
def update(L, n):
L.append(n) # changes the caller's list (mutable)
n = n + 1 # new local int; caller's int unchanged
L = [0] # rebinds local name only
return n
a = [1]
b = 5
print(update(a, b), a, b)def increment(x):
x = x + 1
return x
def append_item(lst):
lst += [99]
n = 5
increment(n)
data = [1, 2]
append_item(data)
print(n, data)Immutable argument (int, str, tuple)
- Function को same object का reference मिलता है
- कोई भी "बदलाव" नया local object बनाता है
- Caller की value नहीं बदलती
Mutable argument (list, dict)
- Function को same object का reference मिलता है
- append, +=, d[k]=v उसी जगह बदलते हैं
- Caller को बदलाव दिखता है
Board-style output questions
def fun(s):
k = len(s)
m = ''
for i in range(0, k):
if s[i].isupper():
m = m + s[i].lower()
elif s[i].isalpha():
m = m + s[i].upper()
else:
m = m + 'bb'
print(m)
fun('school2@com')Trace: lowercase letters uppercase बनते हैं, uppercase lowercase, और हर non-letter (2 और @) "bb" बनता है।
def f(x, y=[]): y.append(x); return y → print(f(1), f(2))। क्यों? (Default lists एक बार बनती हैं।)
Answer: [1, 2] [1, 2] — दोनों calls same default list share करती हैं, जो दोनों appends के बाद print होती है।
सारांश
- return function ख़त्म करता है; return a, b tuple देता है; return नहीं → None।
- Call पर flow function में कूदता है और वापस आता है।
- LEGB lookup; module-level नाम assign करने के लिए global।
- Mutable arguments उसी जगह बदल सकते हैं; immutable नहीं।
