Home › Hindi › functions Hindi में
Python functions Hindi में: def, arguments और return समझें
🌐 यह page Hindi में है · Functions guide in English →
def + returnOutput examplesFree practice
Function क्या है और call कैसे करते हैं?
Function नाम वाला reusable code block है। def से उसे define करो; केवल define करने से body नहीं चलती — नाम के बाद parentheses लगाकर call करना पड़ता है।
def greet(name):
print("Namaste", name)
greet("Asha")
greet("Ravi")
# output:
# Namaste Asha
# Namaste RaviDefinition में name parameter है; call में "Asha" argument है। नाम लिखने में typo या definition run होने से पहले call करने पर NameError आएगा।
return और print एक जैसे नहीं हैं
print screen पर text दिखाता है; return result caller को वापस देता है। बिना return वाला function default रूप से None देता है — output-prediction में बार-बार यही trap आता है।
def square(x):
return x * x
result = square(4)
print(result + 1) # output: 17
def show_square(x):
print(x * x)
value = show_square(4) # output: 16
print(value) # output: NoneCalculation आगे use करनी है तो return दो। return पहुँचते ही function खत्म होता है; उसके बाद उसी block में लिखा statement नहीं चलेगा। इस bug को खुद fix करो।
Positional, keyword और default arguments
Positional arguments का क्रम मायने रखता है; keyword arguments में नाम लिखकर क्रम बदल सकते हो। Default value रखने से argument छोड़ना संभव होता है।
def bill(price, tax=0.05):
return price * (1 + tax)
print(bill(100)) # 105.0
print(bill(price=100, tax=0)) # 100
print(bill(tax=0.2, price=50)) # 60.0Decimal calculation में binary float rounding आ सकती है; currency के लिए production code में decimal.Decimal देखो। Required parameter price न भेजा तो TypeError; गलत keyword लिखा तो भी TypeError।
Local variable और scope
Function में बनी variable बाहर अपने आप उपलब्ध नहीं होती। वही नाम बाहर भी हो तो function की local value अलग है।
count = 10
def increase(count):
count += 1
return count
print(increase(count)) # 11
print(count) # 10Original variable बदलना हो तो returned value assign करो: count = increase(count)। Function के अंदर outer variable को assign करने पर Python उसे local मानता है; उसे पढ़ने की कोशिश करने पर UnboundLocalError आ सकता है। Global बदलने के बजाय parameters और return से data पास करना आसान है।
खुद लिखो: even जाँचने वाला function
एक is_even(n) function बनाओ जो n % 2 == 0 return करे। print(is_even(8)) पर True और print(is_even(7)) पर False आना चाहिए। अगर सिर्फ print(n % 2 == 0) किया तो caller को None मिलेगा। Code browser में run करो, फिर functions learning path पर और exercises करो।
🛠️ Practice करो (free, browser में)
पढ़ना काफी नहीं — यही bug खुद fix करके देखो। नीचे के problems browser में ही चलते हैं, कोई install नहीं, कोई login नहीं।
- Missing return — print और return का फर्क
- Missing argument — parameters गिनो
- Default values — argument का default trace करो
अक्सर पूछे जाने वाले सवाल
Python में function कैसे बनाते हैं?
def name(parameters): से function define करें, indented body लिखें और फिर name(arguments) से call करें।
Python function से कुछ return न करें तो क्या मिलता है?
Caller को None मिलता है। print केवल screen पर output देता है, value वापस नहीं करता।
Parameter और argument में क्या अंतर है?
Definition में नाम parameter होता है, जबकि call के समय भेजी गई actual value argument होती है।