Programming में function एक बहुत महत्वपूर्ण concept है, जो code को व्यवस्थित (organized), reusable और efficient बनाता है।
जब किसी program में एक ही काम बार-बार करना होता है, तो उस code को बार-बार लिखने के बजाय हम उसे एक function में define कर सकते हैं और जरूरत पड़ने पर call कर सकते हैं।
इससे program छोटा, साफ और समझने में आसान हो जाता है।
Function क्या है
Function program का एक ऐसा block of code होता है, जो किसी specific task को perform करता है।
इसे एक बार define किया जाता है और कई बार call किया जा सकता है।
Function का basic syntax
def function_name():
statement
Function को call करने के लिए:
function_name()
Example
def greet():
print("Hello Student")
greet()
Output
Hello Student
Function कैसे काम करता है
- पहले function define किया जाता है (
defkeyword से) - उसके अंदर code लिखा जाता है
- जब function call किया जाता है, तब वह code execute होता है
Function के प्रकार (Types of Functions)
Python में functions को मुख्य रूप से दो भागों में बांटा जाता है:
- Built-in Functions
- User-defined Functions
1. Built-in Functions
ये Python में पहले से बने हुए functions होते हैं।
हमें इन्हें define करने की जरूरत नहीं होती।
Examples
print("Hello")
len("Python")
type(10)
2. User-defined Functions
ये वे functions होते हैं जिन्हें programmer खुद define करता है।
def add():
print(2 + 3)
add()
User-defined Functions के प्रकार
User-defined functions को आगे 4 types में divide किया जाता है:
- Without arguments and without return value
- With arguments and without return value
- Without arguments and with return value
- With arguments and with return value
1. Without arguments and without return value
def greet():
print("Hello")
greet()
Explanation
इसमें कोई input नहीं लिया जाता और कोई value return नहीं होती।
2. With arguments and without return value
def greet(name):
print("Hello", name)
greet("Rahul")
Explanation
इसमें function argument लेता है, लेकिन return कुछ नहीं करता।
3. Without arguments and with return value
def get_number():
return 10
x = get_number()
print(x)
Explanation
इसमें function value return करता है, लेकिन argument नहीं लेता।
4. With arguments and with return value
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Explanation
यह सबसे useful type है, जिसमें input भी लिया जाता है और output भी return होता है।
Parameters और Arguments
- Parameter → function define करते समय
- Argument → function call करते समय
def show(name): # parameter
print(name)
show("Amit") # argument
Real-life Example: Salary calculation
def salary(hours, rate):
return hours * rate
pay = salary(8, 100)
print("Salary =", pay)
Explanation
यह function daily salary calculate करता है।
Real-life Example: Area of rectangle
def area(length, width):
return length * width
print(area(5, 3))
Function के फायदे
- Code reuse होता है
- Program readable बनता है
- Debugging आसान होती है
- Large programs manageable बनते हैं
Common mistakes
- function call करना भूल जाना
- return और print में confusion
- arguments गलत देना
Simple complete program
def square(num):
return num * num
n = int(input("Enter number: "))
print("Square =", square(n))
इस topic की मुख्य बात
Function Python में code को modular और reusable बनाने का सबसे महत्वपूर्ण तरीका है।
इस topic में student यह सीखता है:
- function क्या होता है
- types of functions
- parameters और arguments
- return value
- real-life applications