FUNCTIONS: ========== DIGITAL CALCULATOR ARITHMETIC OPERATIONS ======================= TIGONOMETRIC OPERATIONS ======================= INTEGRATIONS ============ DIFFERENTIATIONS ================= LOGERTHIMS ============= ETC. running the application we may have errors tracing of the error from the complex code is really difficult. we should clear that error. to make identify and simplify the error easily, we need a concept called functions. for easy debugging ==> functions are need. what if a function? =================== named block, which we can use to define the functionality to perform. ==> functions can use in real-time like: 1) function definition 2) function call/function invoke function definition: ==================== that: 1) name of the function 2) about parameters length of parameters function can accept the data in terms of parameters/arguments Note: ===== function definition is always at the top of the program. Syntax: def function-name(parameter1, parameter2, parameter3,...): instruction-1 instruction-2 Note: ===== if the function without parameters, def function-name(): instructions function call/function invoke: ============================== note: ==== the function call is always after the function definition. ==> to invoke the function definition (to make perform through the function), we need the function call. Syntax: function-name(value1, value2, value3,...) if the function with '0' parameters: function-name() Note: ===== We can allow to give any number of function calls within the program. Note: ===== 1) we can write any number of function definitions within the program. 2) functions can be with or without parameters 3) functions can define the operation: by taking the data/values through the function call. 4) functions can do: i) take arguments ii) process the arguments iii) can give the results. # function definition & function call # function definition def greets(): print("Hello") print("Good Morning.") def ariths(para1,para2): addition = para1 + para2 subtraction = para1 - para2 multiplication = para1 * para2 print("The Results are:") print(addition) print(subtraction) print(multiplication) # function call # greets() # greets() # greets() for i in range(5): greets() ariths(10,20) =============================================== The functions are two types: 1) Inbuilt Functions/Pre-defined functions ex: len(), print(), input(), sort(), sorted() etc. 2) User-Defined Functions according to the application requirement, the programmer can add the functionality. ====================================== Note: ===== Functions can return a value. Syntax: return value-name # function definition & function call # function definition def greets(): print("Hello") print("Good Morning.") def ariths(para1,para2): addition = para1 + para2 subtraction = para1 - para2 multiplication = para1 * para2 # print("The Results are:") # print(addition) # print(subtraction) # print(multiplication) return addition, subtraction, multiplication # function call # greets() # greets() # greets() for i in range(5): greets() # print(greets()) # result = ariths(10,20) # print("Sum = ",result) # print("The Sum = ",ariths(10,20)) res1,res2,res3 = ariths(10,20) print("Sum = ", res1) print("Minus = ",res2) print("Product = ",res3) print("The Results = ",ariths(10,20)) =============================================== Types of User-defined functions: ================================ four types: 1) functions without parameters and without returning values Syntax: def function-name(): implementation # function without parameters and no return values def Circle(): radius = int(input("Enter a radius:")) PI = 3.1417 area_circle = PI * radius * radius print("The Area of the circle = ",area_circle) Circle() 2) functions without parameters and with returning values Syntax: def function-name(): implementation return value/value-name def findBigger(): a = int(input("Enter a value for a:")) b = int(input("Enter a value for b:")) c = int(input("Enter a value for c:")) if a > b and a > c: bigger = a # return bigger elif b > c: bigger = b # return bigger else: bigger = c # return bigger return bigger biggest = findBigger() print("The Biggest number among three integers = ",biggest) 3) functions with parameters and no returning values Syntax: def function-name(p1, p2, p3,...): implementation # function with parameters and no return value def Triangle(a,b,c): s = (a + b + c)/2 area = (s * (s-a) * (s-b) * (s-c)) ** 0.5 print("The Area of the Triangle = ",area) Triangle(7,8,9) 4) functions with parameters and with returning values Syntax: def function-name(p1, p2, p3,...): implementation return values/value-names ex: return a, b, c def Triangle(a,b,c): if a == b and a == c and b == c: type = "An Equilateral Triangle." elif a == b or a == c or b == c: type = "An Isosceles Triangle." else: type = "A Scalene Triangle." return type print("The Type of the Triangle = ",Triangle(7,8,9))