Python में Function क्या है? – Python Function in Hindi

Function एक विशेष प्रकार का code block होता है, जिसमें एक या एक से अधिक statements शामिल रहते हैं। Functions का इस्तेमाल करके हम code को सही ढंग से reuse और organize कर सकते है। 

अगर हमें किसी कोड को बार बार execute  करने की जरूरत पड़ती है तो हम उस कोड का एक function बना सकते हैं और जब भी जरूरत पड़ती है तो function को call कर सकते हैं। इससे हमें बार बार कोड लिखने की जरूरत नहीं पड़ती। इससे duplicate code की समस्या से निजात मिलता है। 

Function को कैसे Define करें?

Python में function को define करने के लिए def कीवर्ड का इस्तेमाल करते हैं। def के बाद function का नाम और parentheses () लगाते हैं, जिनमें हम parameters भी दे सकते हैं। फिर function की body आती है।

Function Define करने का Syntax:

def function_name(parameters):

    # Function body

    # Code to be executed

Function का Example

मान लीजिए हमें दो numbers को add करने के लिए एक function बनाना है:

def add_numbers(a, b):

    sum = a + b

    return sum

यहाँ add_numbers फंक्शन को दो पैरामीटर a और b दिए गए हैं, और function के अंदर इन दोनों numbers का sum किया गया है। इसमें return कीवर्ड का इस्तेमाल result को return करने के लिए किया जाता है।

Function को Call (कॉल) कैसे करें?

Function को call करने के लिए बस function का नाम और arguments (parameters) pass करने होते हैं।

Example:

result = add_numbers(5, 3)

print(result)

Output: 8

यहाँ हमने add_numbers फंक्शन को call किया है और 5 और 3 को arguments के रूप में pass किया है। function ने इन values का sum किया और result को return किया।

Types of Python Functions in Hindi – फंक्शन के प्रकार

Python में बहुत प्रकार के functions होते हैं, जिनके बारें में नीचे दिया गया है:-

1. Built-in Functions

Built-in Functions पायथन में पहले से मौजूद होते हैं और इनका इस्तेमाल किसी भी program में सीधे किया जा सकता है।

Examples of Built-in Functions:

  • print() → इसका इस्तेमाल Output को screen पर display करने के लिए करने के लिए किया जाता है।
  • len() → इसका इस्तेमाल किसी sequence (जैसे string, list) की length पता करने के लिए किया जाता है।
  • type() → इसका इस्तेमाल Variable का data type पता करने के लिए किया जाता है।
  • input() → इसका इस्तेमाल User से input लेने के लिए किया जाता है।

Example:

name = input("Enter your name: ")

print("Length of your name is:", len(name))

2. User-defined Functions

ये functions यूजर के द्वारा create किए जाते हैं। इन्हें किसी विशेष कार्य को perform करने के लिए define किया जाता है।

Example:

def greet(name):

    print(f"Hello, {name}! Welcome to Python.")

greet("Yugal")

3. Anonymous Functions (Lambda Functions)

Anonymous functions ऐसे functions होते हैं जिनका कोई नाम नहीं होता। इन्हें lambda कीवर्ड का इस्तेमाल करके define किया जाता है। ये single-line functions होते हैं और ये एक expression को evaluate करते हैं।

Example:

square = lambda x: x * x

print("Square of 5 is:", square(5))

4. Recursive Functions

Recursive functions वो functions होते हैं जो खुद को call करते हैं। इन्हें ज्यादातर mathematical problems को solve करने के लिए इस्तेमाल किया जाता है, जैसे factorial, और Fibonacci series आदि.

Example: Factorial using Recursion

def factorial(n):

    if n == 1:

        return 1

    else:

        return n * factorial(n - 1)

print("Factorial of 5 is:", factorial(5))

5. Generator Functions

Generator functions वो functions होते हैं जो yield कीवर्ड का इस्तेमाल करके values के एक sequence (क्रम) को return करते हैं।

Example:

def generate_numbers():

    for i in range(1, 4):

        yield i

for num in generate_numbers():

    print(num)

6. Higher-Order Functions

Higher-order functions वो functions होते हैं जो एक function को parameter के रूप में लेते हैं या एक function को return करते हैं।

Example:

def apply_function(func, value):
    return func(value)

result = apply_function(lambda x: x * x, 5)
print("Result is:", result)

7. Special Functions (Magic or Dunder Functions)

Python में कुछ special functions होते हैं जिन्हें double underscores (__) से start और end किया जाता है। इन्हें magic methods या dunder methods भी कहते हैं।

Example:

class Example:

    def __init__(self, name):

        self.name = name

    def __str__(self):

        return f"Example object with name {self.name}"

obj = Example("Python")

print(obj)
function in python in hindi

निवेदन:– अगर आपको यह पोस्ट useful लगी हो तो इसे अपने friends के साथ जरूर share कीजिए। और आपके इससे related कोई सवाल हो तो नीचे comment करके बताइए।

Leave a Comment