Python User Defined Functions

Introduction

In Python, functions are reusable blocks of code that perform a specific task. User-defined functions allow you to write your own custom functions to organize your code better and avoid repetition. Functions can accept input parameters, perform operations, and return results.

Python में functions पुन: उपयोग करने योग्य कोड के ब्लॉक होते हैं जो एक विशिष्ट कार्य करते हैं। User-defined functions से आप अपने कस्टम functions बना सकते हैं जिससे आपका कोड व्यवस्थित और पुनरावृत्ति से बचा जा सकता है। Functions इनपुट पैरामीटर ले सकते हैं, ऑपरेशन कर सकते हैं, और परिणाम वापस कर सकते हैं।

Function Syntax

A function is defined using the def keyword, followed by the function name, parentheses (), and a colon :. The function body is indented below the definition.

Function को def keyword से define किया जाता है, उसके बाद function का नाम, parentheses (), और colon : आता है। Function का body indent करके लिखा जाता है।

def function_name(parameters):
    # function body
    statements

Example: Simple Function

def greet():
    print("Hello, welcome to Python functions!")

greet()

This function greet() prints a welcome message when called.

यह greet() function कॉल करने पर एक स्वागत संदेश प्रिंट करता है।

Hello, welcome to Python functions!

Function Parameters and Arguments

Functions can accept inputs called parameters (or arguments) which allow them to perform tasks with different data.

Functions इनपुट्स लेते हैं जिन्हें parameters या arguments कहते हैं, जिससे वे अलग-अलग डेटा के साथ काम कर सकते हैं।

def greet(name):
    print("Hello, " + name + "!")

greet("Anita")

Output:

Hello, Anita!

आउटपुट:

Hello, Anita!

Return Values

Functions can return values back to the caller using the return statement.

Functions return स्टेटमेंट का उपयोग करके मान वापस कर सकते हैं।

def add(a, b):
    return a + b

result = add(5, 3)
print(result)

Output:

8

आउटपुट:

8

Default Parameter Values

You can assign default values to parameters. If the caller does not provide those arguments, the default values are used.

आप parameters को default मान दे सकते हैं। यदि caller उस argument को नहीं देता, तो default मान उपयोग होता है।

def greet(name="Guest"):
    print("Hello, " + name + "!")

greet()
greet("Ravi")

Output:

Hello, Guest! Hello, Ravi!

आउटपुट:

Hello, Guest! Hello, Ravi!

Keyword Arguments

You can call functions using parameter names explicitly, allowing arguments in any order.

आप parameter नाम का उपयोग करके functions कॉल कर सकते हैं, जिससे arguments किसी भी क्रम में दिए जा सकते हैं।

def info(name, age):
    print(f"Name: {name}, Age: {age}")

info(age=30, name="Sunita")

Output:

Name: Sunita, Age: 30

आउटपुट:

Name: Sunita, Age: 30

Variable Length Arguments

Functions can accept arbitrary number of arguments using *args for positional and **kwargs for keyword arguments.

Functions *args का उपयोग करके अनिश्चित संख्या में position arguments, और **kwargs का उपयोग करके keyword arguments स्वीकार कर सकते हैं।

def sum_all(*args):
    total = 0
    for num in args:
        total += num
    return total

print(sum_all(1, 2, 3, 4))

Output:

10

आउटपुट:

10

Function Scope

Variables declared inside a function are local to that function and cannot be accessed outside it. Variables outside functions are global.

Function के अंदर घोषित variable सिर्फ उसी function के लिए local होते हैं और बाहर access नहीं हो सकते। Function के बाहर variables global होते हैं।

def demo():
    x = 10  # local variable
    print(x)

demo()
# print(x)  # Error: x is not defined outside the function

Output:

10

आउटपुट:

10

Why Use Functions?

  • Code reuse and organization
  • Break complex problems into smaller tasks
  • Make code easier to read and maintain
  • Avoid repetition
  • कोड पुन: उपयोग और व्यवस्थित करना
  • जटिल समस्याओं को छोटे भागों में तोड़ना
  • कोड को पढ़ने और बनाए रखने में आसान बनाना
  • दोहराव से बचना