Function Part_1

  •  It is nothing but a group of related instructions to perform a particular task called function.

  • def and return keyword is used in function. 


  Syntax:
            def fun_Name(parameters):
                      Inst1
                      Inst2

                        .
                      Instn
                      return (optional)
Example:
                  def yourName():

                         print(“My name is chaitanya”)


Parameters:
 For Parameters we can say this is the input data to the function.

 

def yourName(name):

    print("My Name is {}".format(name))

 

yourName("Chaitanya")

 

return Statement

   - whenever function done with their operation and  want to return any value that time we use return Statement

   

def full_Name(first_Name,last_Name):

    fullName = first_Name + " " + last_Name

    return fullName

 

name = full_Name("Chaitanya","Patil")

print(name)

 

 

def check_even_Odd(num):

    if num%2 == 0:

        return True

    else:

        return False

print(check_even_Odd(98))

return Multiple values from function

  c,c++,Java languages can't support return Multiple values from function.But Python allows function to return multiple values

  

def math_operatoion(num1,num2):

     add = num1 + num2

     sub = num1 - num2

     mul = num1 * num2

     div = num1/num2

     return add,sub,mul,div

 

add,sub,mul,div = math_operatoion(55,11)

print(add,sub,mul,div)

print(type(math_operatoion(55,11)))

 

Types of arguments

  1.Formal arguments

  2.Actaul arguments

  

def sum(num1,num2):#Formal Arguments

    return num1+num2

 

add = sum(11,22)#Actual Arguments

print(add)

 

2.Actaul arguments

   #There are four types of actual arguments

       A.Positional  arguments

       B.Keyword arguments

       C.Default arguments

       D.Variable length arguments

A.Positional  arguments

  • Need to pass in correct position to function.

  • number of arguments and postional arguments must be matched

  • If position change then result also change.

 

def subtraction(num1,num2):

      return num1 - num2

 

print(subtraction(300,100))

print(subtraction(100,300))

 

B.Keyword arguments

  • we pass value with keyword that is parameter name

  • order not mattered but number of arguments must be matched

  • we can use both postional and keyword arguments together but first we have to use positional keyword order required

 

def addition(num1,num2):

      return num1 + num2

 

print(addition(num2=300,num1=100))

#print(addition(100,num1=300))

print(addition(100,num2=300))

#print(addition(num1=300,100))

 

C.default arguments

  • able to provide a default value to formal arguments that is optional argument

  • after default argument not take non default argument

 

#def multiplication(num1 = 100,num2):

     # return num1 * num2

     

def multiplication(num1,num2 = 100):

    return num1 * num2

print(multiplication(100))

#print(multiplication(100,num1=300))

print(multiplication(100,num2=300))

#print(addition(num1=300,100))

 

D.Variable length arguments

  • we can pass number of arguments to the function using *.

  • able to mix positional and variable together

def sum(*n):

    total = 0

    for num1 in n:

        total += num1

    print(total)

    

sum(11,22)

sum(11,22,33)

sum(11,22,33,66)

 

 

def mul11(aa,*n):

    total = 0

    for num1 in n:

       total += num1

       

    print(total)

mul11(11,56,88)

 

 # After variable length argument,if we are taking any other arguments then we  should provide values as keyword arguments

 

def div(*n,num):

    for num1 in n:

        print(num1)

    print(num)

 

div(11,22,33,44,num=22)

 

#we are able to write keyword variable length argument for that we need to use **. we not able to write any other argument after that

 

def ope(num,**n):

   for num1 in n:

       print(num1)

   print(num)

 

ope(n1=33,n2=44,n3=88,num=100)

 

'''def ope(**n,num):

for num1 in n:

    print(num1)

print(num)'''

 

def ope(**n):

   for key,value in n.items():

       print(value)

       

ope(n1=33,n2=44,n3=88,num=100)

 

Categories: Python Tags: #Python,

Comments