FUNCTIONS ========= RETURNING MULTIPLE VALUES FROM FUNCTION ======================================== # Returning Multiple Values from function def ariths(): d1 = int(input("Enter a value:")) d2 = int(input("Enter a value:")) s_add = d1 + d2 s_sub = d1 - d2 s_prod = d1 * d2 return s_add,s_sub,s_prod res1,res2,res3 = ariths() print("The Addition is = ",res1) print("The Subtraction is = ",res2) print("The Product is = ",res3) ========================================= Ways to Represent the arguments in function =========================================== 4-ways to represent arguments/parameters in function: 1) positional Arguments ======================== # Positional Arguments def function(a,b,c,d): # a,b,c and d ==> Formal Arguments if a == b == c == d: print("It is a Square.") area_square = a ** 2 return area_square elif a == c and b == d: print("It is a Rectangle.") area_rectangle = a * c return area_rectangle else: print("Not possible to calculate any") return "None" p = 9 q = 7 r = 9 s = 7 area = function(11,11,11,11) area1 = function(p,r,s,q) # p,q,r and s ==> Actual Arguments print("The Area is = ",area) print("The Area is = ",area1) ============================================== 2) Keyword Arguments ==================== # Keyword Arguments def printGreetings(name): print("Hi,",name,"Good Morning") printGreetings("Ravi") n = input("Enter the name of the candidate:") printGreetings(name = n) # argument is name, for the argument we have assigned value from 'n' ============================== def rect(length,breadth): circum = 2 *(length + breadth) return circum l = int(input("Enter a length value:")) b = int(input("Enter a breadth value:")) result = rect(length = l,breadth = b) res1 = rect(length = l, breadth = l) print(result) print(res1) 3) Default Arguments ===================== # Default Arguments def printGreetings(name = "user"): print("Hi",name,",Have a great day.") printGreetings() # without any value to the parameter printGreetings("Rakesh") 4) Variable Length Arguments ============================ # Variable Length Arguments def Summing(*args): total = 0 for i in args: total += i print("The Total = ",total) Summing() # function call with 0 arguments Summing(10) # function call with 1 argument Summing(100,200,300,400,500) Summing(1,2,3,4,5,6,7,8,9,10) ==================================== Scope of Variables: =================== Two types of scopes: 1) Local Scope ==> Local Variables/function variables ======================================================= ==> are always allowed to define inside the function. scope ==> within the same function. # Types of Variables def fun(): a = 10 b = 20 # local Variables print(a,b) ========================= fun() # print(a);print(b) def fun(): a = 100 b = 200 for i in range(5): c = 10 res = i + c print(res) print(c) fun() 2) Global Scope ==> Global Variables ====================================== the variables from outside the function ==> Global variables we can access in anywhere of the program # Global Variables a = 10 b = 20 def fun1(): global a,b a = 100 b = 200 print(a) print(b) def fun2(): print(a) print(b) print(a) print(b) fun1() fun2() print(a) print(b) ======================================= Recursion: ========== A function that calls itself again and again is known as "recursion". that function ===> Recursive function. # Recursion # Program to find the factorial of a number using recursion """ 5! = 5 * 4 * 3 * 2 * 1 5! = 5 X fact(4) ==> 5 X 4 X fact(3) ==> 20 X 3 X fact(2) ==> 60 X 2 X fact(1) ==> 120 X 1 X fact(0) ==> 120 X 1 ==> 120 """ def factorial(num): if num == 0: fact = 1 else: fact = num * factorial(num-1) return fact print("The Factorial of 5 is = ",factorial(5)) print(factorial(7))