अभी तक हमने देखा है कि हम बिना function के भी program बनाते हैं लेकिन हमें function क्यों use करना चाहिए अब हम ये जानते हैं। Function एक subprogram होता है जैसे कि जब हम किसी भी प्रकार की application बनाते हैं और वहाँ पर हम कुछ छोटे और कुछ बहुत बड़े program बनाते हैं और जो बड़ा program होता है उसी को छोटे-छोटे part में divide कर लेते हैं वही छोटे-छोटे part subprogram कहलाते हैं और subprogram को ही हम function कहते हैं। जहाँ पर हमें Function की जरूरत होती है वहाँ पर हम function को call करा लेते हैं।
Python Functions Advantages:-
- एक Function को हम बार-बार call करा सकते हैं जहाँ हमें जरूरत हो जिससे कि हमें code बार-बार लिखना नहीं पड़ता।
- Function में code maintain करना ज्यादा आसान है।
- Function में बड़े program को छोटे-छोटे प्रोग्राम(subprogram) में divide कर लेते हैं इसका फायदा ये है कि कहीं भी program में Error आता है तो हमें आसानी से पता लगा सकते हैं कि कौन से subprogram (Function) में Error आया है और आसानी से उसको सही कर सकते हैं।
- Function Programmer का समय, Program की space और memory बचाता है।
- Function में कोई भी नया feature किसी भी time बड़ी आसनी से Add or Remove किया जा सकता है बिना code को effect किये।
Types of Function:-
- Built-In/Predefined Function
- User-Defined Function
1. Built-In/Predefined Function
जो Function Python के द्वारा दिये गये हैं जिनको हमने खुद नहीं बनाया बस हम उनका use करते हैं Built-In/Predifined function कहलाते हैं। जैसे print(), upper() etc.
2. User-Defined Function
User-Defined function वो होते हैं जिनको user अपनी need के according खुद बनाता है। Python में user-defined function में दो प्रकार पड़ते है |
- Function Definition
- Function Call
2.1 Function Definition
Syntax: def function_name(parameter(s)) function_body return statement
- def : Python में function को create करने के लिये ‘def’ keyword का इस्तेमाल किया जाता है।
- function_name : def keyword के बाद function का नाम दिया जाता है। जिसको user अपनी need के according कुछ भी दे सकता है वैसे function का नाम उसके statement से related हो तो अच्छा रहता है।
- (parameter(s)) : Function के अन्दर parameters optional होते है function के name के बाद parenthesis(()) दिया जाता है। उन parenthesis में एक या एक से ज्यादा parameters दिए जाते है।
- : : parenthesis के बाद colon(:) लगाना जरूरी होता है। colon देने के बाद Python interpreter द्वारा automatic code indent होता है |
- function_body : ये function की body होती है। जिसमे कुछ local variables और statements हो सकते है। body में दिए हुए variables को global भी बनाया जा सकता है।
- return statement : ये return statement होता है। return statement के लिए ‘return’ keyword का इस्तेमाल किया जाता है। इस statement से function end होता है। अगर return statement दिया नहीं जाता है तो default ‘None’ return होता है।
Example of Function definition def funpython(comp): #function name, parameter and colon print(func.__doc__) #function body return comp #return statement
Example में ‘funpython’ ये function का नाम है |
उसके बाद parenthesis(()) में ‘comp’ नाम का एक parameter और parenthesis के बाहर colon(:) दिया गया है।
उसके बाद function की body में class attribute की मदद से docstring को print किया गया है।
और आखिर में return statement में ‘comp’ इस parameter को print किया गया है।
2.2 Function Call
Function के call में सिर्फ function का नाम और अगर function का parameter हो तो parameter की value दी जाती है। जब तक function को call नहीं किया जाता तब तक function का code execute नहीं होता है।
Example: #Function definition def funython(comp): #function name, parameter and colon print(func.__doc__) #function body return param #return statement print("Call 1") print(funpython(5)) #Function call print("Call 2") print(funpython(10)) #Function call Output: Call 1 5 Call 2 10
Python में जब function को call करके जिस data type की value as a argument दी जाती है तब उसका data type decide हो जाता है |
Example: def funpython(num, str): print(num) print(str) print("Function Call 1 :") funpython("Hello", 2) print("Function Call 2 :") funpython(2, "Hello") Output: Function Call 1 : Hello 2 Function Call 2 : 2 Hello
User-defined Functions without argument and without return (Practical):-
Program to add to numbers using function without argument def add(): #Function Definition x=int(input("Please enter the value of x: ")) y=int(input("Please enter the value of y: ")) z=x+y print("Addition of two number is:",z) add() #Function call Output:- Please enter the value of x: 5 Please enter the value of y: 7 Addition of two numbers is: 12
User-defined Functions with argument and without return:-
Argument के केस में variables में input function के बाहर लिया जाता है और उन variables को Argument के रूप में calling statement में पास किया जाता है।
Program to add to numbers using function with argument def add(x,y): #Function Definition z=x+y print("Addition of two number is: ",z) x=int(input("Please enter the value of x: ")) y=int(input("Please enter the value of y: ")) add(x,y) #Function call Output:- Please enter the value of x: 8 Please enter the value of y: 7 Addition of two numbers is: 15
User-defined Functions with argument and with return value:-
Return के केस में answer को function के बाहर भेजते हैं जिसे कोई अन्य variable ग्रहण करता है और फिर
उसको Print करता है।
Program to add two numbers using function with argument and return statement def add(x,y): z=x+y return z a=int(input("Please enter the value of a: ")) b=int(input("Please enter the value of b: ")) c=add(a,b) print("Addition of two numbers is: ",c) Output:- Please enter the value of a: 10 Please enter the value of b: 12 Addition of two numbers is: 22
User-defined Functions with multiple return values:-
Python में function multiple values को भी return कर सकता है जिसमें वह returning values का
एक sequence return करता है जिसे हम बाद में प्राप्त करके एक एक करके print करा सकते हैं।
Program which return multiple values def calc(x,y): add=x+y sub=x-y mul=x*y div=x/y return add,sub,mul,div a=int(input("Please enter the value of a: ")) b=int(input("Please enter the value of b: ")) p,q,r,s=calc(a,b) print("Addition of two numbers is: ",p) print("Difference of two numbers is: ",q) print("Multiplication of two numbers is: ",r) print("Quotient of two numbers is: ",p) Output:- Please enter the value of a: 12 Please enter the value of b: 6 Addition of two numbers is: 18 Difference of two numbers is: 6 Multiplication of two numbers is: 72 Quotient of two numbers is: 18
Functions में Parameters और Arguments:-
• जब हम किसी function का header लिखते हैं तो उसके parantheses अर्थात कोष्ठक ‘( )’ में दी जाने वाली एक
या एक से अधिक values को Parameter कहा जाता है।
• ये वह values होती हैं जिन्हे function किसी कार्य के लिए use करता है।
• जबकि argument वह value है function को call करते समय कोष्ठक (parantheses ) में लिया जाता है।
• दूसरे शब्दों में कहें की function को invoke करने के लिए arguments दिए जाते हैं।
• Formal parameter को parameter तथा actual parameter को argument कहा जा सकता है।
Function Argument in Python:-
Python में चार प्रकार के function parameters होते है |
- Default Argument
- Required Argument
- Keyword Argument
- Variable Number of Argument
1. Default Argument
Default argument में definition में argument के लिए default value ‘='(assignment operator) के द्वारा set की जाती है।
Function Call पर जब definition में assign किया हुआ argument नहीं दिया जाता तो default value pass की जाती है।
Example: def defaultarg(num=3, str="Hello"): print(num) print(str) print("Call 1") defaultarg() print("Call 2") defaultarg(5) print("Call 3") defaultarg(5, "Hii") Output: Call 1 3 Hello Call 2 5 Hello Call 3 5 Hii
2. Required Argument
Required Argument में function call में argument देना अनिवार्य होता है |
जब function definition में argument दिया जाता है तब उसे function call में उसकी value देना required होता है।
Example: def requiredarg(num, str): print(num) print(str) print("Call 1") ReqArg(3, "Hello") print("Call 2") ReqArg("Hello", 3) Output: Call 1 3 Hello Call 2 Hello 3
Note:- अगर function call में argument दिया नहीं जाता है तो , TypeError का exception आ जाता है।
3. Keyword Argument
अब तक function में positional argument का इस्तेमाल किया गया है। Positional argument ये normal parameters होते है। positional argument में जब values दी जाती है तब उसकी position की हिसाब से call पर value assign की जाती है। Keyword Argument में function call में दिया जाता है। Keyword Argument की मदद से arguments की positions को बदला जाता है।
Example: def keywordarg(int, float, str): print("int :",int) print("float :",float) print("String :",str) print("Call 1") keywordarg(int=10, float=1.5, str="H") print("Call 2") keywordarg(float=2.8, int=20, str="G") print("Call 3") keywordarg(str="R", float=8.6, int=5) Output: Call 1 int : 10 float : 1.5 String : H Call 2 int : 20 float : 2.8 String : G Call 3 int : 5 float : 8.6 String : R
4. Variable Number of Arguments
कभी-कभी हमें ये नहीं होता कि function पर कितने arguments pass करने है। ऐसी situation में ‘Number of Arguments’ का महत्व काफी बढ़ जाता है। जब number of argument का इस्तेमाल करना हो तो Function के definition पर argument से पहले ‘*'(asterisk) का इस्तेमाल किया जाता है। ‘Function definition में दिया हुआ argument ‘tuple’ जैसा होता है।
Example: def multiargu(*argTuple): print(argTuple) print(argTuple[0]) print(argTuple[1]) print(argTuple[2]) for i in argTuple: print(i) multiarfu(3, 7, "Hello") Output: (3, 7, 'Hello') 3 7 Hello 3 7 Hello
Anonymous or Lambda Function
Anonymous or lambda function को सारी programming language support नहीं करती हैं। Python में जब एक Normal Function create किया जाता है तब function कोई एक विशेष नाम दिया जाता है। लेकिन Anonymous या lambda Function कक कोई नाम नहीं होता है। Normal Function के लिए ‘def’ keyword का इस्तेमाल किया जाता है लेकिन Anonymous/Lambda Function के लिए ‘lambda’ keyword का इस्तेमाल किया जाता है। ये function काफी छोटा होता है।
Syntax: lambda arguments:expression
arguments : lambda function में एक या एक से ज्यादा arguments हो सकते है।
expression : lambda function में एक ही expression होता है | expression; return भी होता है।
Example: double = lambda x: x * 3 print(double(7)) Output: 21
इन्हें भी देखें।
- while loop
- for loop
- nested loops
- loop control statements (Break, Continue, Pass)
- if statement
- if-else statement
- if-elif statement
- nested if statement
- range() function
- keywords
- Identifiers (names)
- Literals
- Operators
- punctuatorts
Python Programs के लिये यहाँ click करें।