0% found this document useful (0 votes)
3 views3 pages

Def Function

Functions in Python are reusable blocks of code defined using the 'def' keyword, allowing for easier understanding and maintenance. A function consists of a name, optional parameters, a body of code, and may include a return statement to output results. Functions help avoid code repetition, as demonstrated by examples of printing greetings and calculating sums.

Uploaded by

krish125461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Def Function

Functions in Python are reusable blocks of code defined using the 'def' keyword, allowing for easier understanding and maintenance. A function consists of a name, optional parameters, a body of code, and may include a return statement to output results. Functions help avoid code repetition, as demonstrated by examples of printing greetings and calculating sums.

Uploaded by

krish125461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

def function ka basic concept:

def ka use Python mein function banane ke liye hota hai. Function ek aisa block hota hai
jisme hum code likhte hain jo bar-bar use kiya ja sakta hai. Function ka purpose hai code ko
reusable aur samajhne mein aasaan banana.

---

Structure of a Function:

1. def keyword: Function ko define karne ke liye.

2. Function Name: Function ka naam (jo aap rakhna chaho).

3. Parentheses (()): Input values (parameters) ke liye. Yeh optional hote hain.

4. Colon (:): Bataata hai ki function start ho raha hai.

5. Function Body: Isme wo kaam likhte hain jo function karega.

6. return statement (optional): Result ko function se bahar bhejne ke liye.

---

Example:

def say_hello(): # Function define kiya


print("Hello, everyone!") # Function ka kaam likha

Iska output tab dikhega jab isse call karoge:

say_hello() # Function ko call kiya

Output:
Hello, everyone!

---

Why use functions?

Agar tumhe ek kaam bar-bar karna hai, toh function ka use karke code baar-baar likhne se
bach sakte ho.

Example: Tumhe 10 baar "Hello" print karna hai.

def say_hello():
print("Hello!")

# Ab bas function ko baar-baar call karo:


for _ in range(10):
say_hello()

---

….
….

….

Ek function banate hain jo 2 numbers ka sum kare:

def add_numbers(a, b): # Function define kar rahe hain


sum_result = a + b # Add kar rahe hain
return sum_result # Result ko return karenge

Is function ko call karke hum result le sakte hain:

result = add_numbers(10, 20) # Function call hua


print("Sum:", result) # Result print kiya

Output:

Sum: 30

---
Key Points (Simplified):

1. Function Define Karna:


Code ko reusable banane ke liye ek naam ke andar kaam likhna:

def naam(parameters):
# yahan code likhte hain

2. Function Call Karna:


Jab kaam chahiye, tab function ka naam likh ke call karte hain:

naam(values)

3. Return Statement:
Agar function result wapas de raha ho, toh return ka use karo. Example:

def square(x):
return x * x
print(square(4)) # Output: 16

---

You might also like