15 Function in python

1. Using functions we can give a name to the block of code.

2. Using functions we can re use the code.

3. Using def keyword we can create a function.


# Use of Parameters 

1. Using parameters we can make a function dynamic

2. Using parameters we can pass values to the function

3. python function support 4 types of parameters

    1. Required or positional arguments   add(a,b)-------add(3,4)

    2. Keyword arguments---------add(a,b)--------add(b=3,a=7)

    3. Default arguments---------add(a,b=3)------add(6)

    4. Variable length arguments(Args, Kwargs)---args  *a,   kwargs   **a


### Simple function

#function creation

def add():

    print( 3 + 6 )

# function call

add()


1)  required arguments.

'''

1. We should give values to the required arguments.

2. The given values pass in correct positional order.

'''


def add(a,b):

    print(' A value is :', a)

    print(' B value is :', b)


add(4, 6)

#add(5, 8)

#add(42, 98)

2) Key word arguments

'''

1. We can pass values based on parameter name.

2. Keyword arguments should follow positional argument.


'''


def add(a, b, c):

    print(' A value is :', a)

    print(' B value is :', b)

    print(' C value is :', c)


#add(4, 7)


#add(b=7,a=6)


##  Doesn't accept multiple values for single parameter

#add(4,a=78)


add(78, b=8, c=4)


3) Default arguments

'''

1. Using default arguments we can give default value to the parameter.

2. Default arguments should follow the positional argument.

'''


def add(a, b, c=9):

    print(' A value is : ', a)

    print(' B value is : ', b)

    print(' C value is : ', c)


add(7, 8)


# Length arguments

'''

1. using args(*) we can takes N number of arguments

2. it will take 0 to many values in tuple format

'''

def add( a, b=5, *c):

    print(' A value is : ', a)

    print(' B value is : ', b)

    print(' C value is : ', c)


add(3, 67, 7,54,'sri')



add(7, 8, 4,6,3)


# length arguments kwargs

'''

1. using kwargs(**) we can take values in dictionary format

'''

def add(a, b=5,*c, **d):

    print(' A value is : ', a)

    print(' B value is : ', b)

    print(' C value is : ', c)

    print(' D value is : ', d)


add(6,7,3,67,9, name='sriram', eid=56)


# function with return statement

'''

1. Return keyword used to return value from function.

2. After executiong return keyword function will terminate

3. We can use many return keywords in side a function

'''


def add(a=10,b=25):

    print(' Before return')

    if b > 31 : return  'ok'

    print(' After return')


ret = add(20,25)

print(' return value is :', ret)


#function with multiple return statements

'''

1. Return keyword used to return value from function.

2. After executiong return keyword function will terminate

3. We can use many return keywords in side a function

'''


def add(a=20,b=30):

    if a > 13: return 'ok'

    print(' Before return')

    return a + b

    print(' After return')

    if a == 20: return 'sriram'


#print(add(15))


ret = add()

print(' return value is :', ret)













  

Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

16 file handling in python

4 Tuple data types