FUNCTIONS :

Function is sub-routine, which is design to perform a specific task. Functions ccontains 3 parts and they are as follows:

 

        1. FUNCTION DECLARATION :

Simply declaring a function.

Ex-1:

def add(arguments):

 statements-1

 statements-2

 statements-3

add(arguments)

 

 

        • POSITIONAL ARGUMENT :

In positional arguments order is important.

Ex-1:

def add(a,b):

  print(a+b)

def sub(a,b)

  print(a-b)
 
add(2,3)      #5

sub(10,10)    #0

 

Ex-2:

def add ():

  a=10

  b=20

  print(a+b)

def sub() :

  print(a-b)

add()

sub()


        • Write a program to find add, sub,mul of 2 integers. Using function.
def calc(a,b) :
print(a+b)
print(a-b)
print(a*b)
calc(2,3)   #6, -1,6
calc(10,20)   # 30,-10,200
2)  KEYWORD ARGUMENT :

In this keyword argument order is not important. Here, positional argument is followed by keyword argument.

Ex-1:
def add(a,b) :
 print(a+b)
def sub(a,b)
 print(a-b)
add(a=10,b=20)  #30
sub(b=20,a=10)  #-10
add(b=1,a=3)   #4
sub(a=10, b=25)  #-15

 

Ex-2 :

def add(a,b):
 print(a+b)
def sub(a,b):
 print(a-b)
add(10,a=20)  #ERROR
sub(b=20,15)  #ERROR
sub(10,b=15)   #-5

 

3)  DEFAULT ARGUMENT :
Ex-1:
def add(a,b=20)
 print(a+b)
add(10,50)  #60 [It overrides]
add(10)    #30
4) VAR-ARG PARAMTER :

We can pass multiple values for a single variable. Var-Arg variable is tuple object.
(*t).

Ex-1 :
def add(*t)
 c=a+b
 return c
print(add(10,20))

 

Ex-2:

def add(a,b)
 c=a+b
 print( c)
print(add(10,20)

 

Ex-3:

def add(*t)
 s=0
 for x in t :
   s=s+x
   print(s)
add(10)   #10
add(10,20)   #30
add(10,20,30)  #60
add(10,20,30,40)  #100




RECURSIVE FUNCTION

 

A function which call itself is called recursive function.

  • Write a program to find the factorial of 1 using recursive function:
def fact(n):

  if x==0:

      r=1

  else:

      r=n*fact(n-1)

return(r)

print(fact(5))


                ANONYMOUS FUNCTION:

If we declare a function without a name it I called “anonymous function”.

Syntax:

lamda inputvalue:expression

Example:

s=lamda x=x*x

print(s(4))

We can pass function as argument

  • FILTER()

Filter function can take one argument as function and another argument as any sequence .  Filter function can return the value if expression is true. Filter function can returns filter object o we have to convert into list object.

Ex-1:

l=[20,3,5,6,8,15,100,11]

l2=list(filter(lamda x=x%2==0,l))

print(l2)

  • MAP FUNCTION:

Map function can take function a one argument and sequence as other argument.

This function will return map object ,so we have to convert into list object. This Map () returns value for each expression.

 

Ex-1:

l=[20,3,5,6,8,11,15,100]

l2=list(map(lamda x=x+x,l))

print(l2)

 

Ex-2:

l1=[10,20,30,40,50,60]

l2=[20,8,5,6,11,15,100,8]

l3=list(lamda x,y=x+y,l1,l2)

print(x,y)

 

3)REDUCE:

It return as a single object,

Ex-1:

import functool

l1=[20,3,5,6,11,8,15,100]

l2=functool.reduce(lamda x,y=x+y,l)

print(l2)

 

 

 

 

 

 

 

 

Related Posts