FUNCTIONS ========= PYTHON FUNCTIONAL APPROACH ==> C SCRIPTING ==> PERL MODULAR APPROACH ==> MODULA3 calculator arithmetic =========== trigonometric ============= log ==== integration etc. =========== function can be a block of code which can be represented with some name and we can use this to perform the specific task. ==> the function cannot execute by itself. by invoking/calling the function it can perform the task. Advantages: 1) solving the complex or larger programs easily. 2) debugging the application is easy. How to define the function? =========================== keyword: def Syntax: def functions-name(parameters/arguments): block of statements How we can call the function? ============================= Syntax: function-name(values for parameters) Types of Functions ================== 1) Functions without parameters and no return type =================================================== Syntax: def function-name(): function body # WRITING A FUNCTION WITHOUT PARAMETERS AND NO RETURN TYPE # function definition def findSmall(): # function header # function body a = int(input("Enter a value:")) b = int(input("Enter a value:")) c = int(input("Enter a value:")) d = int(input("Enter a value:")) if a < b and a < c and a < d: print("The Smaller value is = ",a) elif b < c and b < d: print("The Smaller value is = ",b) elif c < d: print("The Smaller value is = ",c) else: print("The Smaller value is = ",d) # function call findSmall() 2) Functions without Parameters and return type ============================================== Syntax: def function-name(): function-body return value # WRITING A FUNCTION WITHOUT PARAMETERS AND RETURN TYPE from importlib.resources import read_binary def decToBin(): number = int(input("Enter a decimal:")) # 97 n = number binary = "" while n != 0: rem = str(n % 2) # 1 0 binary = rem + binary # "1100001" n //= 2 return binary # result = decToBin() # print(result) print(decToBin()) ============================================== # WRITING A FUNCTION WITHOUT PARAMETERS AND RETURN TYPE from importlib.resources import read_binary def decToBin(): number = int(input("Enter a decimal:")) # 97 n = number binary = 0 power = 1 while n != 0: rem = n % 2 # 1 0 binary = binary + rem * power # "1100001" power *= 10 n //= 2 return binary # result = decToBin() # print(result) print(decToBin()) 3) Functions with parameters and no return type =============================================== Syntax: def function-name(parameter-names with comma separation): function body # WRITING A FUNCTION WITH PARAMETERS AND NO RETURN TYPE def triangleArea(s1,s2,s3): s = (s1 + s2 + s3)/2 area = (s*(s-s1)*(s-s2)*(s-s3)) ** 0.5 print("The Area of the Triangle is = ",area) # triangleArea(9,7,5) # s1 = 10 # s2 = 8 # s3 = 7 # # triangleArea(s1,s2,s3) a = 9 b = 7 c = 8 triangleArea(c,a,b) 4) Functions with parameters and return type ============================================ Syntax: def function-name(parameters with comma separation): function body return # WRITING A FUNCTION WITH PARAMETERS AND RETURN TYPE def typeTriangle(a,b,c): if a == b and a == c and b == c: return "It is an Equilateral Triangle." elif a == b or a == c or b == c: return "It is an Isosceles Triangle." elif (a * a + b * b) == c * c or (b * b + c * c) == a * a or (a * a + c * c) == b*b: return "It is a Right Angled Triangle." else: return "It is a scalene Triangle." # print(typeTriangle(10,10,10)) # a = 9 # b = 7 # c = 9 # print(typeTriangle(a,b,c)) # p = 5 # q = 4 # r = 3 # print(typeTriangle(p,q,r)) print(typeTriangle(10,11,9))