Python Functions क्या हैं? Function, Arguments, Parameters & Return | Python Functions in Hindi | RBSE Class 12
Python Function क्या है?
Function program में किसी विशेष कार्य को करने वाला reusable code block होता है। Function की मदद से हम एक ही code को बार-बार लिखने के बजाय उसे एक बार define करके आवश्यकता के अनुसार कई बार use कर सकते हैं।
उदाहरण के लिए यदि हमें बार-बार दो numbers का sum निकालना है, तो हम उसके लिए एक function बना सकते हैं।
Functions की आवश्यकता क्यों होती है?
- Code को reusable बनाने के लिए।
- Program को छोटे-छोटे logical parts में divide करने के लिए।
- Code को समझना और maintain करना आसान बनाने के लिए।
- Repeated code को कम करने के लिए।
- Program की readability बेहतर करने के लिए।
Python Functions के प्रमुख प्रकार
| Type | उदाहरण |
|---|---|
| Built-in Functions | print(), len(), input(), type() |
| User-defined Functions | जो programmer स्वयं बनाता है |
Python में Function कैसे बनाते हैं?
Python में user-defined function बनाने के लिए def keyword का उपयोग किया जाता है।
Syntax
def function_name():
statements
Simple Function Example
def greet():
print("Hello Python")
greet()
Output:
Hello Python
यहाँ def से function define किया गया है और
greet() लिखकर function को call किया गया है।
Function Define और Function Call
Function बनाने की प्रक्रिया को function definition कहा जाता है और function को execute करने के लिए उसे call किया जाता है।
def welcome():
print("Welcome to Python")
welcome()
यहाँ:
def welcome():— function definitionwelcome()— function call
Function with Parameter
Function को input देने के लिए parameters का उपयोग किया जाता है।
def greet(name):
print("Hello", name)
greet("Rahul")
Output:
Hello Rahul
Parameter क्या है?
Function definition में दिए गए variables को parameters कहा जाता है।
def greet(name):
print("Hello", name)
यहाँ name parameter है।
Argument क्या है?
Function को call करते समय जो actual value दी जाती है उसे argument कहा जाता है।
greet("Rahul")
यहाँ "Rahul" argument है।
Function with Two Parameters
def add(a, b):
print(a + b)
add(10, 20)
Output:
30
Multiple Parameters
def student(name, age, course):
print("Name:", name)
print("Age:", age)
print("Course:", course)
student("Amit", 18, "Python")
return Statement
return statement का उपयोग function से कोई value वापस भेजने के लिए किया जाता है।
Example
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output:
30
print और return में अंतर
| print() | return |
|---|---|
| Value को screen पर display करता है। | Function से value वापस भेजता है। |
| Returned value को आगे calculation में directly use करना इसका मुख्य उद्देश्य नहीं है। | Returned value को variable में store करके आगे use किया जा सकता है। |
Return के साथ Example
def square(num):
return num * num
result = square(5)
print("Square =", result)
Output:
Square = 25
Function में Multiple Values Return करना
Python में एक function से multiple values return की जा सकती हैं।
def calculate(a, b):
return a + b, a - b, a * b
x, y, z = calculate(10, 5)
print("Addition =", x)
print("Subtraction =", y)
print("Multiplication =", z)
Default Argument
यदि function parameter को पहले से कोई default value दी जाती है तो उसे default argument के रूप में उपयोग किया जा सकता है।
def greet(name="Student"):
print("Hello", name)
greet()
greet("Amit")
Output:
Hello Student
Hello Amit
Keyword Arguments
Function call करते समय parameter के नाम के साथ value देने को keyword argument कहा जाता है।
def student(name, age):
print(name, age)
student(age=18, name="Ravi")
यहाँ arguments को parameter names के साथ दिया गया है।
Positional Arguments
जब arguments को उनकी position के अनुसार pass किया जाता है तो उन्हें positional arguments कहा जाता है।
def student(name, age):
print(name, age)
student("Ravi", 18)
Local Variable क्या है?
Function के अंदर बनाया गया variable सामान्यतः उस function के local scope में होता है।
def show():
x = 10
print(x)
show()
यहाँ x function के अंदर बनाया गया variable है।
Global Variable क्या है?
Function के बाहर बनाया गया variable global scope में हो सकता है और उसे appropriate scope rules के अनुसार function के अंदर access किया जा सकता है।
x = 100
def show():
print(x)
show()
Output:
100
Built-in Functions
Python में कई functions पहले से उपलब्ध होते हैं। इन्हें Built-in Functions कहा जाता है।
| Function | उपयोग |
|---|---|
| print() | Output display करना |
| input() | User से input लेना |
| len() | Length जानना |
| type() | Object का type जानना |
| int() | Integer conversion |
| float() | Float conversion |
| str() | String conversion |
User-defined Function
जो function programmer स्वयं def keyword की मदद से बनाता है,
उसे User-defined Function कहा जाता है।
def multiply(a, b):
return a * b
print(multiply(5, 4))
Output:
20
Function में if Statement का उपयोग
def check_number(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Zero"
print(check_number(10))
Output:
Positive
Function में Loop का उपयोग
def print_numbers(n):
for i in range(1, n + 1):
print(i)
print_numbers(5)
Output:
1
2
3
4
5
Even या Odd Check करने का Function
def check_even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
print(check_even_odd(10))
print(check_even_odd(7))
Output:
Even
Odd
Factorial Function
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
print(factorial(5))
Output:
120
Largest Number का Function
def largest(a, b):
if a > b:
return a
elif b > a:
return b
else:
return "Both are equal"
print(largest(20, 15))
Output:
20
Calculator using Functions
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Addition =", add(a, b))
print("Subtraction =", subtract(a, b))
print("Multiplication =", multiply(a, b))
print("Division =", divide(a, b))
Function में *args
जब function को variable number of positional arguments देने हों तो
*args का उपयोग किया जा सकता है।
def total(*numbers):
result = 0
for num in numbers:
result += num
return result
print(total(10, 20, 30))
print(total(1, 2, 3, 4, 5))
Output:
60
15
Function में **kwargs
Variable number of keyword arguments के लिए **kwargs का
उपयोग किया जा सकता है।
def student_info(**details):
for key, value in details.items():
print(key, ":", value)
student_info(name="Amit", age=18, course="Python")
Python Function का Basic Structure
def function_name(parameters):
# function body
return value
Functions के फायदे
- Code reusability बढ़ती है।
- Program modular बनता है।
- Code duplication कम होता है।
- Debugging आसान हो सकती है।
- Program को समझना आसान होता है।
- Large programs को छोटे parts में divide किया जा सकता है।
Function और Loop में अंतर
| Function | Loop |
|---|---|
| Reusable code block बनाने के लिए। | Code को repeatedly execute करने के लिए। |
| Function को call करने पर execute किया जाता है। | Loop अपनी condition/iteration के अनुसार चलता है। |
Common Mistakes in Python Functions
1. Function Call करना भूल जाना
def greet():
print("Hello")
ऊपर function define किया गया है, लेकिन execute करने के लिए
greet() call करना होगा।
2. return और print को confuse करना
यदि value को function से बाहर उपयोग करना है तो अक्सर
return की आवश्यकता होती है।
3. Parameter की संख्या का ध्यान न रखना
def add(a, b):
return a + b
add(10, 20)
Function में आवश्यक parameters के अनुसार arguments देना चाहिए।
4. Indentation की गलती
Function body में statements सही indentation के साथ लिखे जाने चाहिए।
Python Functions Quick Revision
| Concept | अर्थ |
|---|---|
| def | Function define करने का keyword |
| Function Call | Function को execute करना |
| Parameter | Function definition में variable |
| Argument | Function call के समय दी गई value |
| return | Function से value वापस भेजना |
| Default Argument | Parameter की default value |
| Local Variable | Local scope में variable |
| Global Variable | Global scope में variable |
| *args | Variable positional arguments |
| **kwargs | Variable keyword arguments |
RBSE Class 12 Important Questions
- Function क्या है?
- Python में function की आवश्यकता क्यों होती है?
- Python में function define करने के लिए किस keyword का उपयोग किया जाता है?
- Function definition और function call में अंतर लिखिए।
- Parameter और argument में अंतर लिखिए।
- return statement क्या है?
- print() और return में अंतर लिखिए।
- Built-in और User-defined functions क्या हैं?
- Default argument क्या है?
- Positional और keyword arguments क्या हैं?
- Local और global variable क्या हैं?
- *args क्या है?
- **kwargs क्या है?
- दो numbers का sum करने वाला function लिखिए।
- Number even या odd check करने वाला function लिखिए।
- Factorial निकालने वाला function लिखिए।
- Calculator बनाने के लिए functions का उपयोग कीजिए।
Frequently Asked Questions (FAQs)
Python Function क्या है?
Function reusable code block है जिसे किसी specific task को perform करने के लिए बनाया जाता है।
Python में function बनाने के लिए कौन-सा keyword उपयोग होता है?
User-defined function बनाने के लिए def keyword का उपयोग
किया जाता है।
Parameter और Argument में क्या अंतर है?
Parameter function definition में दिया गया variable होता है जबकि argument function call के समय दी जाने वाली actual value होती है।
return statement का क्या उपयोग है?
return statement function से value वापस भेजने के लिए उपयोग किया जाता है।
Built-in Function क्या है?
Python में पहले से उपलब्ध functions जैसे print(), len(), input() और type() को built-in functions कहा जाता है।
अब अगला Topic पढ़ें
Python functions की विस्तृत जानकारी के लिए Python की official documentation देखें।