In Python, function arguments are the values or variables that you pass to a function or method when you call it. These arguments are used to specify the parameters of the function or method, and they are an important part of Python programming. There are several types of function arguments in Python, including positional arguments, keyword arguments and default arguments. Each of these types of arguments serves a specific purpose and can be used in different ways to customize the behavior of a function or method.

Positional, keyword, and default arguments are important concepts in Python programming that allow you to specify the arguments or parameters that a function or method should accept. Understanding how to use these types of arguments effectively can help you write more flexible and reusable code in Python.

 

Positional Arguments

Positional arguments are the arguments that are passed to a function or method in a specific order. The order in which you pass these arguments matters, because the function or method will use the arguments in the order that they are received. For example, if you have a function that takes two arguments, the first argument will be used as the first parameter, and the second argument will be used as the second parameter.

To use positional arguments, the arguments need to be passed in the same order as their respective parameters in the function definition.

 

def getgrade(name, score):
    """ This function computes a grade given a score"""
    if score > 80:
        grade = 'A'
    elif 80 > score > 70:
        grade = 'B'
    elif 70 > score > 60:
        grade = 'C'
    else:
        grade = 'D'

    return name + " had grade: " + grade

 

To call this function using positional arguments:

getgrade('prince', 78)                  # prince had grade: B

The getgrade('prince', 78) statement automatically passes 'prince' to the name parameter and 78 to the score parameter.

 

getgrade(78, 'prince')          # ERROR

This function call is not the same as getgrade('prince', 78) because this statement passes 78 to name and 'prince' to score. And since the score parameter is supposed to be an integer but a string is passed to, it will raise an error and halt the program.

That's all for positional arguments: note the arguments need to be passed in the same order as their respective parameters in the function definition.

 

Keyword Arguments

Keyword arguments are arguments that are passed to a function or method using the name of the argument followed by an equal sign and the value of the argument. These arguments do not need to be passed in a specific order, because the function or method will use the names of the arguments to determine which values to use for which parameters.  i.e passing each argument in the form name = value.

We are going to be using the same getgrade() function to demonstrate the use of keyword arguments.

To call the getgrade() function using keyword arguments:

getgrade(name='prince', score=78)       # prince had grade: B

 

This is very clear, we are assigning 'prince' to name and 78 to score. The exciting part of using keyword arguments is you can mix the order in which you provide the arguments.

getgrade(score=78, name='prince')        # prince had grade: B

The arguments can appear in any order, unlike positional arguments.

 

Mixing Positional and Keyword arguments

It is possible to mix positional arguments with keyword arguments

But the positional arguments cannot appear after any keyword arguments have been defined.

For example, if you have a function header as:

def f(p1, p2, p3, p4):


You can invoke it by using:

f(21, p2=43, p3=11)


It would be wrong to invoke by:

f(p1=21, 43, 11)

because the positional arguments 43 and 11 appears after the keyword argument p1=21.

 

Default Arguments

Default arguments are arguments that are given a default value when the function or method is defined. These arguments are optional, because the function or method will use the default value if no value is provided for that argument when the function is called. Default arguments can be used to provide default values for optional parameters, or to specify default behavior for a function or method. Python allows us to create functions with parameters that have default values.

The default values are passed to the parameters when the function is invoked without the arguments.

Let's define a function with default arguments.

def area_perimeter(width=2, height=2):
    area = width * height
    perimeter = (2 * width) + (2 * height)

    print("Area = " + str(area) + "  and  Perimeter = " + str(perimeter))

The width parameter has a default value of 1 and height has a value of 2. If this function is called without one or any of the arguments. it will automatically pass the default values as the arguments.

 

Various ways to call this function:

area_perimeter()            # this function call uses the default arguments

area_perimeter(10, 5)       # positional arguments width=10 height=5

area_perimeter(width=10)     # this will set height to use the default value, 2

area_perimeter(height=4, width=45)      # keyword arguments

area_perimeter(15)           # sets width = 15 and height uses the default value

 

Program Output:

Area = 4  and  Perimeter = 8
Area = 50  and  Perimeter = 30
Area = 20  and  Perimeter = 24
Area = 180  and  Perimeter = 98
Area = 30  and  Perimeter = 34

 

In summary, positional, keyword, and default arguments are important tools in Python that allow you to specify the arguments or parameters that a function or method should accept. Understanding how to use these types of arguments effectively can help you write more flexible and reusable code in Python.