← Back to Python Course | Chapter 4: Functions | Lesson 1 of 9

Python Functions का परिचय

Function एक ऐसी recipe की तरह है जिसे आप एक बार लिखते हैं और जब चाहें इस्तेमाल करते हैं। आप उसे एक नाम देते हैं, और हर बार जब आप वह नाम बुलाते हैं, Python उन steps को follow करता है।
Syntax
python
def function_name():
    # body

function_name()

Function क्या है?

Function def keyword से define किया गया एक नाम वाला, reusable code का block है जो सिर्फ़ तभी चलता है जब उसे explicitly call किया जाए -- logic को function में bundle करने का मतलब है इसे एक बार लिखना और code को बार-बार दोहराने के बजाय जितनी बार चाहें invoke करना।

उदाहरण: What is a Function?

python
def greet():  # defines a reusable function named greet
    print("Hello!")

greet()  # first call
greet()  # second call, runs the same code again

Function को Call करना

def greet(): लिखना सिर्फ़ function की definition को interpreter के साथ register करता है; इसके body के अंदर कुछ भी तब तक execute नहीं होता जब तक आप अलग से इसे call करने के लिए parentheses के साथ greet() न लिखें।

उदाहरण: Calling a Function

python
def greet():
    print("Hi there")

# Nothing runs until greet() is called
greet()

docstrings

docstring एक string literal है जिसे किसी function की body की सबसे पहली line पर रखा जाता है, यह बताते हुए कि function क्या करता है; help() जैसे tools और ज़्यादातर IDEs इसे अपने-आप दिखाते हैं, जिससे यह एक साधारण comment से कहीं ज़्यादा उपयोगी बन जाता है।

उदाहरण: docstrings

python
def greet():
    """Prints a friendly greeting."""
    print("Hello!")

print(greet.__doc__)  # __doc__ holds the function's docstring text

Functions में Control Flow

किसी function की body में Python के सभी constructs हो सकते हैं -- conditionals, loops, और अन्य functions की calls -- यानी functions एक line जितने simple हो सकते हैं या अंदर काफ़ी substantial logic को coordinate कर सकते हैं।

उदाहरण: Control Flow in Functions

python
def classify(n):
    if n % 2 == 0:
        return "even"
    else:
        return "odd"

print(classify(4))  # calls the function and prints its return value

Functions का Dynamic Execution

क्योंकि Python में functions first-class objects हैं, आप किसी function को variable में assign कर सकते हैं, कई functions को list में रख सकते हैं, या एक function को दूसरे function के argument के रूप में pass कर सकते हैं -- यानी behavior को खुद एक value की तरह इधर-उधर ले जा सकते हैं, सिर्फ़ data की तरह नहीं।

उदाहरण: Dynamic Execution of Functions

python
def greet():
    print("Hello!")

say_hi = greet  # functions are values, so greet can be assigned to another name
say_hi()  # calling say_hi runs the same code as greet()
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.