Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐍 Unit 1 – Python Revision और Functions  ·  Lesson 03

Functions: Return, Scope और Flow

Values return करना

return statement caller को value वापस भेजता है और function तुरंत ख़त्म कर देता है। Function एक value, कई values (tuple के रूप में) या कुछ नहीं return कर सकता है।

Python
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)
Hello Asha None 81 (2, 9) 2 9
Python
def check(n):
    if n % 2 == 0:
        return 'even'
        print('never printed')     # unreachable after return
    return 'odd'

print(check(4), check(7))
even odd

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 पर लौटता है।

Python
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 11
Start Sum 7 End
order →1510671237811
Lines चलने का क्रम: 1 → 5 → 10 → 6 → 7 → 1 → 2 → 3 → 7 → 8 → 11

Variables का scope (LEGB)

Built-inprint, len, range, max …Globalprogram के top level पर defined नामEnclosingबाहरी function के नाम (nested functions के लिए)Localcurrent function के अंदर बने नाम, parameters सहित
Python Local → Enclosing → Global → Built-in में ढूँढता है
Python
x = 'global'

def outer():
    x = 'enclosing'
    def inner():
        print(x)          # not local -> enclosing found first
    inner()

outer()
print(x)
enclosing global

global keyword

Function के अंदर global variable पढ़ना allowed है। उसे assign करने के लिए global declare करें।

Python
count = 10

def double():
    global count        # refer to the global variable
    count = count * 2

double()
double()
print(count)
40

UnboundLocalError

अगर function अपनी body में कहीं भी किसी नाम को assign करता है, तो वह नाम पूरे function में local है। Assignment से पहले उसे पढ़ना error है:

Python
c = 10
def g():
    print(c)     # c is local here because of the next line
    c = 5
g()
Output
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 भेजना

Python
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)
6 [1, 5] 5
Python
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)
5 [1, 2, 99]

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 को बदलाव दिखता है
Caller के variable का क्या होता है

Board-style output questions

Python
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')
SCHOOLbbbbCOM

Trace: lowercase letters uppercase बनते हैं, uppercase lowercase, और हर non-letter (2 और @) "bb" बनता है।

✅ Try

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 नहीं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

LEGB rule क्या है?
Python नाम को इस क्रम में ढूँढता है: Local (current function के अंदर), Enclosing (बाहरी function), Global (module level), Built-in (print, len जैसे नाम)। पहला match use होता है।
क्या function एक से ज़्यादा values return कर सकता है?
हाँ। return a, b tuple (a, b) देता है, जिसे unpack कर सकते हैं: x, y = f()।
Python call by value है या call by reference?
ठीक-ठीक कोई नहीं — यह object references भेजता है। Function के अंदर mutable object (list, dict) में बदलाव बाहर दिखता है; parameter re-assign करना या immutable object बदलना नहीं।
← Class 12 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicException Handling (try-except) आपकी प्रगति: 3 / 26 (12%) · Lesson 4 / 26 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें