Functions: ========== -> Functions can always define in outside the class only. If you can define a function inside the class then that definition is considered as method. -> When we want to implement modular approach, we need to understand the functions. -> Function is a named block which we can use to define the certain functionality. Ex: Calculator arithmetic operations trigonometric operations etc. -> to define the function in python: we have keyword: "def" Syntax: def functionName/identifier(parameters/arguments): function body group of statements -> when we have define the function, that function must be invoked/called. Syntax for the function call: =============================== functionName(values for parameters) # HOW TO CREATE THE FUNCTION WITHOUT PARAMETERS def stringPalindrome(): # function header string = input("Enter a string:") reverseString = "" for i in string: reverseString = i + reverseString # "e" + "l" ==> "v" + "el" ==> "vel" # print(reverseString) if reverseString == string: print(string,"is palindrome") else: print(string,"is not palindrome") stringPalindrome() ======================================================= # HOW TO WRITE A FUNCTION WITH PARAMETERS def findSmall(a,b,c,d): if a < b and a < c and a < d: print(a,"is smaller") elif b < c and b < d: print(b,"is smaller") elif c < d: print(c,"is smaller") else: print(d,"is smaller") findSmall(11,101,-1,100) ========================================== -> We ca categorize the parameters of the function into two types: 1) Formal parameters/Formal arguments 2) Actual parameters/Actual arguments -> When parameters can write within the function definition are called as "formal arguments". -> When parameters can write within the function call are called as "actual parameters". The Ways to define actual arguments: ==================================== -> 4-ways: 1) Positional arguments 2) Keyword arguments # HOW TO WRITE A FUNCTION WITH PARAMETERS def findSmall(a,b,c,d): if a < b and a < c and a < d: print(a,"is smaller") elif b < c and b < d: print(b,"is smaller") elif c < d: print(c,"is smaller") else: print(d,"is smaller") findSmall(11,101,-1,100) # positional arguments p = int(input("Enter some value:")) # -11 q = int(input("Enter some value:")) # -33 r = int(input("Enter some value:")) # 99 s = int(input("Enter some value:")) # 101 # keyword argument representation findSmall(a = p,b = q,c = r,d = s) # findSmall(d = p,b = s,a = r, c = s) # -11, 101, 99, 101 3) Default arguments ===================== # Default argument representation with functions def sumNumbers(a = 0,b = -1,c = 1,d = 0,e = 2): s = a+b+c+d+e print("The sum = ",s) sumNumbers() # no values for parameters sumNumbers(10,20) sumNumbers(11,22,33,44,55) 4) Variable length arguments ============================= # variable length arguments def variableLengthArguments(*args): s = 0 for i in args: s = s + i print("The Sum of any number of parameters = ",s) variableLengthArguments() variableLengthArguments(123) variableLengthArguments(11,22,33,44) variableLengthArguments(11,101,12,121,131,101,111) ===================================================== def function(*a): for i in a: print(i) function() function("python") function("Python","Java","JavaScript","Micro Services") ==================================================== Return type: =========== when we want to make a function with returning a value of any type, we can use "return" keyword. Syntax: return value return v1,v2,v3,.. # function with return type def f1(): return 0,1,2 x,y,z = f1() print(x,y,z)