🟢 Beginner  ·  Lesson 10

Python में Functions

Function क्या है?

Function code का दोबारा use होने वाला block है जो एक काम करता है। एक बार लिखो, कई बार call करो। इससे programs छोटे, organized और ठीक करने में आसान रहते हैं।

💡 Functions क्यों ज़रूरी

एक ही 5 lines को 10 जगह copy करने के बजाय, उन्हें function में रखें और call करें। एक fix हर जगह update — यही clean B.Tech-level code का दिल है।

Function Define करना

def keyword, एक नाम, brackets और colon use करें। indented block body है।

Python – def.py
def say_hello():
    print("Hello!")

say_hello()    # call करें
say_hello()    # फिर call करें
Hello! Hello!

Parameters और Arguments

Function inputs ले सकता है (parameters)। आप जो values देते हैं वे arguments हैं।

Python – param.py
def greet(name):
    print("Hello,", name)

greet("Aman")
greet("Riya")
Hello, Aman Hello, Riya

Return Values

return result को caller के पास वापस भेजता है ताकि उसे store या reuse किया जा सके।

Python – ret.py
def square(n):
    return n * n

result = square(5)
print(result)
25

Program 1: Greeting Function

Python – greet_fn.py
def greet(name, time):
    print(f"Good {time}, {name}!")

greet("Aman", "morning")
greet("Riya", "evening")
Good morning, Aman! Good evening, Riya!

Program 2: Area Calculator

Python – area.py
def rectangle_area(length, width):
    return length * width

def circle_area(radius):
    return 3.14 * radius * radius

print("Rectangle:", rectangle_area(10, 5))
print("Circle   :", circle_area(7))
Rectangle: 50 Circle : 153.86

दो छोटे functions, हर एक एक calculation करके answer लौटाता है।

Program 3: Default और Keyword Arguments

Python – defaults.py
def power(base, exp=2):     # exp default 2
    return base ** exp

print(power(5))           # default exp=2
print(power(2, 3))        # exp=3
print(power(exp=4, base=2))  # keyword args
25 8 16
  • exp=2 default है — न देने पर इस्तेमाल होता है।
  • Arguments को नाम (keyword) से किसी भी order में दे सकते हैं।

Program 4: Factorial Function

Python – fact_fn.py
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print("5! =", factorial(5))
print("0! =", factorial(0))
5! = 120 0! = 1

Program 5: *args — कितने भी Inputs

Python – args.py
def total(*numbers):       # *args सबको tuple में जमा करता है
    return sum(numbers)

print(total(10, 20))
print(total(1, 2, 3, 4, 5))
30 15

*numbers function को कितनी भी values लेने देता है, जो tuple में इकट्ठा होती हैं।

Local बनाम Global Variables

  • Function के अंदर बना variable local है — सिर्फ वहीं रहता है।
  • बाहर बना variable global है — हर जगह दिखता है।
  • अंदर से global बदलने के लिए global keyword use करें।

सामान्य गलतियाँ

  • return भूलना — तब function None देता है।
  • Function define करना पर कभी call न करना।
  • गलत संख्या में arguments देना।
  • print (दिखाता है) और return (वापस देता है) में confusion।

Practice Tasks

  1. एक function लिखें जो दो numbers में बड़ा लौटाए।
  2. एक function लिखें जो check करे number prime है या नहीं।
  3. एक function जो नाम और optional greeting ले (default "Hello")।
  4. *args से किसी भी numbers का average निकालने वाला function।

सारांश

  • Functions def से बनाए reusable code blocks हैं।
  • Parameters inputs लेते हैं; return result वापस भेजता है।
  • Defaults, keyword args और *args functions को flexible बनाते हैं।
  • Function के अंदर के variables default रूप से local होते हैं।
← Back to Python Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.