FUNCTIONS ========= C ==> printf(), scanf(), getchar(), putchar() ==> Functional approach PERL ==> SCRIPTING MODULA3 ==> MODULAR APPROACH CALCI ARITHMETIC ========== TRIGONOMETRIC ============= LOG ==== INTEGRATION =========== DERIVATIVES ============ CLASS EX: class Hello{ public static void main(String x[]) { System.out.println("Hello World!"); } } Function Vs Method: =================== Syntax for function and method are same function ==> can always write outside the class method ==> can always write inside the class. Note: ==== Java programming language has "methods" whereas the python programming language has: both functions and methods. What is the function? ===================== a named block which can be use to define the specific task. function can always be invoked/called. function can accept a data using parameters processing parameters return the output. How to define functions in Python? ================================== keyword: def def function-name(parameter names with comma separation): block of code for specific task How to call/invoke the function? ================================= function-name(data) Ways to define functions ======================== ==> there are 4-ways: 1) function without parameters and no return type ==================================================== Syntax: def function-name(): # function header function body # WRITING A FUNCTION WITHOUT PARAMETERS AND NO RETURN TYPE def sumDigits(): # function header # function body number = int(input("Enter a decimal value:")) sum_dig = 0 n = number while n != 0: ind_dig = n % 10 sum_dig += ind_dig n //= 10 print("The Sum of individual digits of {} is = {}".format(number,sum_dig)) sumDigits() ============================================================ 2) function without parameters and return type =============================================== Syntax: def function-name(): function-body return value # WRITING A FUNCTION WITHOUT PARAMETERS AND RETURN TYPE def findBig(): a = int(input("Enter a value:")) b = int(input("Enter a value:")) c = int(input("Enter a value:")) d = int(input("Enter a value:")) e = int(input("Enter a value:")) if a > b and a > c and a > d and a > e: big = a return big elif b > c and b > d and b > e: big = b return big elif c > d and c > e: big = c return big elif d > e: big = d return big else: big = e return big # bigger = findBig() # print("The Biggest value is = ",bigger) print("The Biggest number is = ",findBig()) ====================================================== 3) function with parameters and no return type ============================================== Syntax: def function-name(parameter with comma separation): function-body # WRITING A FUNCTION WITH PARAMETERS AND NO RETURN TYPE def typeTriangle(s1,s2,s3): if s1 == s2 == s3: print("It is an Equilateral Triangle.") elif s1 == s2 or s1 == s3 or s3 == s2: print("It is an Isosceles Triangle.") elif (s1 ** 2 + s2 ** 2) == s3 ** 2 or (s1 ** 2 + s3 ** 2) == s2 ** 2 or (s2 ** 2 + s3 ** 2) == s1 ** 2: print("It is a Right Angled Triangle.") else: print("It is a Scalene Triangle.") # typeTriangle(11,11,11) # s1 = 10 # s2 = 11 # s3 = 10 # typeTriangle(s1,s2,s3) # a = 3 # b = 4 # c = 5 # typeTriangle(c,a,b) p = 9 q = 10 r = 11 typeTriangle(s1 = r,s2 = p, s3 = q) 4) function with parameters and return type =========================================== Syntax: def function-name(parameters): function-body return statement # WRITING A PYTHON FUNCTION WHICH CAN ACCEPT PARAMETERS AND RETURNING A VALUE. def findAreaOfTriangle(a,b,c): s = (a + b + c)/2 area = (s * (s-a) * (s-b) * (s-c))**0.5 return area print("The Area of the Triangle is = ",findAreaOfTriangle(7,9,8)) =================================================== Types of Parameters: ==================== Parameters ==> Arguments two types of parameters: 1) Actual Arguments 2) Formal Arguments the arguments which we can define in function header ==> formal arguments the arguments which we can define in function call ==> actual arguments