Python Functions

Functions Python mein reusable blocks of code hote hain jo specific task ko perform karte hain. Yeh code ko organize aur modular banane mein madad karte hain. Neeche kuch important concepts aur examples diye gaye hain jo functions ke use ko samjhate hain:

1. Basic Function Definition:

Function define karne ke liye def keyword ka use hota hai.

python

Python
# Basic function definition
def greet():
    print("Hello, welcome!")

# Function call
greet()  # Output: Hello, welcome!

2. Function Parameters:

Functions parameters accept karte hain jo function ke andar use hota hai.

python

Python
# Function with parameters
def greet(name):
    print("Hello, " + name + "!")

# Function call with argument
greet("Alice")  # Output: Hello, Alice!

3. Return Values:

Functions ek value ko return kar sakte hain return statement se.

python

Python
# Function with return value
def add(a, b):
    return a + b

# Function call and using return value
result = add(5, 3)
print(result)  # Output: 8

4. Default Parameters:

Functions mein default parameters specify kiye ja sakte hain.

python

Python
# Function with default parameter
def greet(name="Guest"):
    print("Hello, " + name + "!")

# Function call without argument
greet()        # Output: Hello, Guest!

# Function call with argument
greet("Alice") # Output: Hello, Alice!

5. Variable Arguments:

Functions mein variable number of arguments ko accept kiya ja sakta hai.

python

Python
# Function with variable arguments
def sum_values(*args):
    total = 0
    for num in args:
        total += num
    return total

# Function call with different number of arguments
print(sum_values(1, 2, 3))             # Output: 6
print(sum_values(1, 2, 3, 4, 5))        # Output: 15

6. Keyword Arguments:

Functions mein keyword arguments specify kiye ja sakte hain.

python

Python
# Function with keyword arguments
def person_info(name, age, city):
    print("Name:", name)
    print("Age:", age)
    print("City:", city)

# Function call with keyword arguments
person_info(age=30, name="John", city="New York")
# Output:
# Name: John
# Age: 30
# City: New York

7. Lambda Functions:

Lambda functions ek anonymous function hoti hain jo ek line mein define hoti hain.

python

Python
# Lambda function
double = lambda x: x * 2

# Using lambda function
print(double(5))  # Output: 10

8. Recursive Functions:

Functions khud ko call karne wale hote hain.

python

Python
# Recursive function to calculate factorial
def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

# Using recursive function
print(factorial(5))  # Output: 120

9. Passing Functions as Arguments:

Functions ko arguments ke roop mein bhi pass kiya ja sakta hai.

python

Python
# Function to perform operation on numbers
def operate_on_numbers(a, b, operation):
    return operation(a, b)

# Function to add two numbers
def add(a, b):
    return a + b

# Function to multiply two numbers
def multiply(a, b):
    return a * b

# Using function as argument
print(operate_on_numbers(5, 3, add))       # Output: 8
print(operate_on_numbers(5, 3, multiply))  # Output: 15

Yeh concepts functions ke use ko understand karne mein madad karte hain aur Python programming ko modular aur efficient banate hain. Functions ko sahi tareeke se use karke code readability aur maintainability improve ki ja sakti hai.

Leave a Reply

Your email address will not be published. Required fields are marked *

Up
Python Framework & Libraries ,यह कर लिया तो आप की लाइफ सेट है Vladimir Putin, the President of Russia educational Qualification cybersecurity top 10 book American women top 10 fitness Sure, here are the 10 most important things about Dhruv Rathee