Posted By Naveen Singh on 24 Mar 2023
python-programPython में function block ऑफ code होते है जो की call करने पर ही run होते है। Function में हम parameters के through data को pass कर सकते हैं। और यह data, function में result के form में return करता है।
Python में हम function को हम def keyword की सहायता से create कर सकते हैं।
Calling a function-: function को call करने के लिए हम function के name को parenthesis के साथ call कराते है।
Syntax-:
def function_name(parameters):
Statement
function_name()
Example-:
def new_function():
print(" This is a new function")
new_function
Output-:
This is a new function
Function में जो information pass किया जाता है, उसे हम argument कहते हैं। arguments को हम function name के बाद parenthesis के अन्दर लिखते है। function में हम एक से ज्यादा
argument add कर सकते हैं, जिसे हम comma से separate करके लिखते है।
Example-:
def new_function(name):
print(name + " good")
new_function("apple")
new_function("mango")
new_function("banana")
Output-:
apple good
mango good
banana good
Function में parameter और argument दोनो का ही use हम information को function में pass करने के लिए करते है। Function के perspective से देखे तो, parameter एक तरह का
variable होता है जिसको हम function definition में parentheses के अन्दर लिखते हैं। Argument function में एक तरह की values होती हैं जो की function call होने पर pass की जाती है।
Number of arguments-: function को हम by default correct number of arguments के through ही call कराना चाहिए।
मतलब की अगर आपके function में दो arguments की requirement है तो आप function को call करते समय दो ही argument pass करोगे न दो से ज्यादा न दो से कम।
Example-:
def new_function(first_, last_name):
print(first_name + " " + last_name)
my_function("Ramesh", "Kumar")
Output-:
Ramesh Kumar
Note-: अगर आप दो से अधिक argument pass करते हो तो वहां पर Error आएगी।
यदि आप नहीं जानते कि आपके function में कितने argument pass किए जाएंगे, तो function definition में parameter नाम से पहले (*) जोड़ें। इस तरह function को arguments का एक tuple
प्राप्त होगा, और accordingly items तक पहुंच सकते हैं।
Example-:
def new_function(*players):
print("The best player is " + players[1])
new_function("kohli", "smith", "warner")
Output-:
The best player is smith
Function में हम argument को key=value syntax के form में भी send कर सकते हैं। इसमें arguments का order matter नही करता है।
Example-:
def new_function(first_player, second_plqyer, third_player):
print("The best player is " +second_player )
new_function(first_player = "Rohit", second_player = "Kohli", third_player = "Marsh")
Output-:
The best player is Kohli
Keyword argument को हम "kwargs" name से python documentation में use करते है।
यदि आप नहीं जानते कि आपके function में कितने keyword argument pass किए जाएंगे, तो आप function definition में parameter name से पहले दो asterisk(**) add कर दे।
इस तरह function को arguments का एक dictionary प्राप्त होगी, और accordingly items तक पहुंच सकता है। अगर आपके number of arguments unknown है तो आप parameter name
से पहले दो asterisk (**) add कर दे।
Example-:
def new_function(**stu):
print("His last name is " + stu["last_name"])
new_function(first_name = "Rohit", last_name = "singh")
Output-:
His last name is singh
आप function में कोई भी data type(string, list, dictionary, tuple) as a argument pass कर सकते है, और ये function के अन्दर same data type की तरह ही treat किए जाते है।
Example-:
def new_function(fruit):
for i in fruit:
print(i)
fruits = ["apple", "mango", "cherry"]
my_function(fruits)
Output-:
apple
mango
cherry
Python में हम function को return करने के लिए return statement का use करते हैं।
Example-:
def new_function(i):
return i * i
print(new_function(3))
print(new_function(5))
Output-:
9
25
Python recursion function को भी accept करता है, जिसका मतलब है कि defined function स्वयं को call कर सकता है। Recursion एक common mathmatical और programming
concept है। जिसका मतलब है कि एक function खुद को call कर करता है। इसका अर्थ यह है कि आप result तक पहुंचने के लिए data के through loop कर सकते हैं।
Example-:
def tri_recursion(r):
if(r > 0):
total = r + tri_recursion(r - 1)
print(total)
else:
total = 0
return total
print("\n\nRecursion Example Results")
tri_recursion(6)
Output-:
Recursion Example Results
1
3
6
10
15
21
इस example में, tri_recursion() एक ऐसा function है जिसे हम ने खुद को call करने के लिए defined किया है ("recursion")। हम ‘r’ variable का उपयोग data के रूप में करते हैं, जो हर बार जब हम recurse करते हैं (-1) घटते हैं। recursion तब समाप्त नही होता है जब condition 0 से greater नहीं होती है (अर्थात जब यह 0 होती है)।
I Hope आपको Python में function क्या होते है? function python में कैसे work करता है, और function के types के बारे में आपको जानकारी मिली होगी। यदि आपको कोई doubt है तो comment section में comment करके पूछ सकते हैं।