User Defined Functions In Python msbte | Function Definition and Calling in python Msbte
User Defined Functions In Python
Any programmer from C, C++ and Java background knows that a function is set of statements whenever called. Or sometimes we create functions to divide business logic into small manageable code to reduce Complexity. A function in C, C++ and Java have return-type: depending on the datatype of returned value/variable. But Python function does not have return-type; although it allows returning (one or more) values). The complete discussion on Python's function is done in this section including defining user-defined function, calling a function, returning values(s), passing parameters and many more.
Function Definition In Python
- A function is a block of code[i.e Contains statements] that performs an certain action and once it is defined, it can be reused n number of times. Functions make code more modular, allowing us to use the same code over and over again, Almost every programs we demonstrated till now, were using many functions like print(), input(), len() and many more. In this section we will discuss about defining creating user-defined function.
- A function is defined by using the keyword ' def ' followed by a name of your choosing, followed by a set of parentheses() which may hold parameters, and ending with a colon [ : ]. For example, if we want to define a function and name it "hello_Cwpedia"; then we can do it as:
- def hello_Cwpedia():
- This creates function header. Now we have to write statements for this function. In case of an IDE, pressing the Enter key will take the cursor to next line with an indent. In case of console/command prompt, pressing the Enter key will take the cursor to next line but manually requires at least one space (or tab) to specify indent and must continue with same space (or tab) count. The function definition ends when the indent is not given. For example,
- print("Hello Cwpedia 1") #General Statement
- print("Hello Cwpedia 2") #General Statement
- def hello_Cwpedia(): #Function Header
- print("Hello Cwpedia 3 ") #Statement1 inside hello_Cwpedia()
- print("Hello Cwpedia 4") #Statement2 inside hello_Cwpedia()
- print("Hello Cwpedia 5") #Ends hello_Cwpedia(),General Statement
- print("Hello Cwpedia 6") #General Statement
- From above block, note that, line-3 starts hello_Cwpedia() definition , line-4 and lines-5 are two statements that are part of hello_Cwpedia() function. These also has indent. But line-6 ends the function definition because it doesn't has indent and written in same vertical axis of general statements in line-1, 2 and 3. Hence, line-6 and line-7 are not part of hello_Cwpedia() Function definition.
- Above user defined function say Hello B defined only for the purpose of discussion of creating function in Python .Similar to other programming languages, Python functions can also receive parameter/arguments and can also return. A function can have any numbers of statements and any logical statements or control flow statements or loops or local variables. For example, refer following two functions :
- def hello_Cwpedia(): #Function To Print "Hello !Welcome To Cwpedia."
- print("Hello !Welcome To Cwpedia.")
- def addtwono_Cwpedia(): #Funtion To Take Two Value From User And ADD Them
- no1=int(input("Enter Number 1:- "))
- no2=int(input("Enter Number 2:- "))
- cal=no1+no2
- print("Addition =",cal)
- def sqrt_Cwpedia(): #Funtion To Take A Value From User and Show The SquareRoot Of It
- no1=int(input("Enter A Number:- "))
- cal= no1 * no1
- print("SquareRoot =",cal)
- For ease of understanding of indents, we applied a vertical axis line. It will clear the start and end of function definition. In similar way, we can define any numbers of functions in a program for above discussed purposes. But remember that, a function never execution if we don't call it.
Function Calling
- We know that, to use a function we have to call it. To call a function, simply write that function's name followed by parenthesis (), and by placing required arguments within the parenthesis. For example, let's call above defined functions and complete that program.
- def hello_Cwpedia(): #Function To Print "Hello !Welcome To Cwpedia."
- print("Hello !Welcome To Cwpedia.")
- def addtwono_Cwpedia(): #Funtion To Take Two Value From User And ADD Them
- no1=int(input("Enter Number 1:- "))
- no2=int(input("Enter Number 2:- "))
- cal=no1+no2
- print("Addition =",cal)
- def sqrt_Cwpedia() #Funtion To Take A Value From User and Show The SquareRoot Of It
- no1=int(input("Enter A Number:- "))
- cal= no1 * no1
- print("SquareRoot =",cal)
- print("Main Area Started")
- hello_Cwpedia() #First Calling to hello_Cwpedia()
- addtwono_Cwpedia() #Then Calling to addtwono_Cwpedia()
- sqrt_Cwpedia() #Then Calling to sqrt_Cwpedia()
- print("Main Area Ended")
- Output :-
- Main Area Started
- Hello !Welcome To Cwpedia.
- Enter Number 1:- 2
- Enter Number 2:- 2
- Addition = 4
- Enter A Number:- 2
- SquareRoot = 4
- Main Area Ended
Post a Comment