Python Functions Arguments


In python, there are following four types of formal arguments :

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable number of arguments

Required arguments :

  • Arguments follow the positional order.

  • The number of arguments and order of arguments in the function call must be the same as that in the function definition.

Example :

# function definition
def myfun(arg1):
    print("Value of arg1 ",arg1)
    return

# function invocation with required argument

myfun() 
                
OUTPUT
TypeError: myfun() missing 1 required positional argument: 'arg1'
                

As you can see int the above program, we are getting the error because the number of arguments is not matching at the time of function call.


Keyword Arguments :

  • When used in function call, calling function identifies the argument by parameter name.

  • Allow us to change the the positional order of arguments.

  • Python interpreter uses the keyword provided to match the values with parameters.

Example :

# function definition
def myfun(name, roll_num):
    print("Name of student ", name)
    print("Roll Number of student ", roll_num)
    return

# function invocation with keyword argument

myfun(name='Daneyel', roll_num=1001)
    #OR
myfun(roll_num=1001,name='Daneyel')  # change the order
                

Default Arguments :

  • At the time of function definition, we set a value for the arguments that serve as default arguments.

  • At the time of function call, if we do not provide the value for that argument, then the default value will be used.

In function definition, non-default parameter must not follow default parameter.

Example:

# function definition
def myfun(product, tax_rate=4, offer):  #ERROR, non-default parameter follows default parameter
    pass
def myfun1(product, tax_rate=4, offer=5):
    pass
                

Program to understand the use of default argument

# function definition
def myfun(product, tax_rate=4):
    print("Product ", product, " with tax ",tax_rate)
# function invocation with keyword argument

myfun(product='Maggi')  # default will use
# OR
myfun(product='Tomato-Ketchup', tax_rate=6)
                
OUTPUT
Product  Maggi  with tax  4  # default used
Product  Tomato-Ketchup  with tax  6
                

Next chapter is Variable Number of Arguments






 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved