Positional and Keyword Arguments: Application and Differences

A Python Tutorial

Overview of Parameters and Arguments

Function parameters are inputs or variables that (some) functions require to execute their code. Put differently, they are the information a function may need to carry out its task. For example, if we want to define a function that calculates (and returns) the area of any rectangular floor, we would need the length and breadth of the floor as parameters.

                         area = length x breadth
def fl_area(length, breadth):
      area = length * breadth
      return area

To be sure, recall that not every function requires parameters, and a function might also use single or multiple parameters as well.

While the terms: parameters and arguments are sometimes conflated to mean the same thing, there is a subtle difference between the two. Given that parameters are variables specified when defining a function, arguments on the other hand are 'actual values' passed in for those parameters when you call the function. For instance, if we want to calculate the area of a rectangular floor of length 50m and breadth of 30m, we can call the function defined above and pass the values 50 and 30 respectively, as arguments.

fl_area(50, 30)

result:

1500

In Python, you can pass arguments in two main ways; as either:

  • Positional arguments, or

  • Keyword arguments

Positional Arguments

Positional arguments are passed in the same order in which the parameters were written in the function definition. Python matches each argument in the function call with a parameter in the function definition.

To see how this works, consider a function that displays the name and age of a student in a class.

def stud_info(name, age):
      """"Display information about student.""""
      print("My name is " + name + "and I am " + age + "years old.")

When we call stud_info(), we need to provide a name and an age, in that order. For a student named John who is 16 years old, the function call will be thus:

stud_info('John', 16)

output:

 My name is John and I am 16 years old.

You can call the function as many times as needed, passing the name and age of each student respectively, as arguments.

stud_info('Anna', 17)
stud_info('Lex', 16)
stud_info('Jessica', 15)

output:

My name is Anna and I am 17 years old.
My name is Lex and I am 16 years old.
My name is Jessica and I am 15 years old.

You can use as many positional arguments as you need in your functions. Python will match each argument you provide with the corresponding parameter in the function's definition.

The key thing to note is that order matters a lot when using positional arguments. Mixing up the order of the arguments might yield unexpected results. See the illustration below.

stud_info(18, 'Mat')

output:

My name is 18 and I am Mat years old.

Read the output again. If you get funny results like this, check to see that you provided the arguments in the correct order.

Keyword Arguments

In keyword arguments, you directly assign a value for the parameters when making the function call, in the form of a name-value pair. Keyword arguments free you from having to worry about correctly ordering your arguments in the function call, and they clarify the role of each value in the function call.

We can rewrite one of the function calls above using keyword arguments as follows:

def stud_info(name, age):
      """"Display information about student.""""
      print("My name is " + name + "and I am " + age + "years old.") 

stud_info(name = 'John', age = 16)

Result:

My name is John and I am 16 years old.

Reversing the order of the arguments yields the same results:

stud_info(age = 16, name = 'John')

output:

My name is John and I am 16 years old.

The order doesn't matter because we have explicitly told Python which parameter each argument should be matched with.

Using Default Values

When defining a function, you can specify a default value for any parameter. If an argument for the parameter is provided in the function call, Python uses the argument value. If not, it uses the parameter's default value. Once a default value has been assigned to a parameter, you can optionally decide to exclude any argument for that parameter when making a function call.

Let's say the average age of students in the class is 17. We might decide to set a default age of 17 for every student in the class thus:

def stud_info(name, age = 17):
      """"Display information about student.""""
      print("My name is " + name + "and I am " + age + "years old." )

In making a function call, we have:

stud_info('Bill')

output:

My name is Bill and I am 17 years old.

This works just the same as:

stud_info(name = 'Bill')

output:

My name is Bill and I am 17 years old.

In both instances, we have excluded an argument for the age parameter but the function runs just fine as Python uses the default value i.e. 17. The argument we included is for the name parameter and we have matched its value via position and keyword respectively. This brings us to a very important caveat.

NOTE: When you use default values, any parameter with a default value needs to be listed after all the parameters that don't have default values. This allows Python to continue interpreting positional arguments correctly.

To use a different age for a particular student, you could use a function call like this:

stud_info(name = 'Bob', age = 18)

output:

My name is Bob and I am 18 years old.

Because an explicit argument for age is provided, Python will ignore or overwrite the parameter's default value.

Lastly, make sure to provide an argument for each defined parameter when making a function call. Using fewer or more arguments would lead to errors.

Hope you enjoyed this article. Follow for more exciting and informative posts on this blog.

Thanks for reading.