Python Functions — Parameters and Arguments

Aleksandar Gakovic
Level Up Coding
Published in
4 min readAug 15, 2020

--

Photo by Shahadat Rahman on Unsplash

This week I’ve been reading all about parameters and arguments. I’ve learnt that there are five types of parameters and two kinds of arguments.
In this article, I will be explaining with custom examples the following:

  • Arguments with default values
  • *args allows us to pass in a variable number of positional arguments
  • **kwargs allows us to pass in a variable number of keyword arguments
Two ships bayed at a landing pad on Cassel [Goss II]

Building a Star Citizen Ship function:

What is the difference between arguments and parameters?

Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what types of arguments a function can accept.
- Python docs

For example, given the function definition:

def star_citizen_ship(max_speed, adjective=None, **kwargs):
pass

max_speed, adjective, and kwargs are parameters of star_citizen_ship.

However, when calling star_citizen_ship, for example:

star_citizen_ship('1,347 m/s', adjective='shiny', action='racing')

The values 1,347 m/s’, ‘shiny’, and ‘racing’ are arguments.

According to the Python glossary arguments come in two forms:

Keyword argument: an argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by **.

Positional argument: an argument that is not a keyword argument. Positional arguments can appear at the beginning of an argument list and/or be passed as elements of an iterable preceded by *.

Cassel [Goss II] Approach

Working with my star_citizen_ship function

def star_citizen_ship(max_speed, adjective='shiny',
action='zooming', model='Origin 350r'):

print(f'The max speed of the {model} is {str(max_speed)}.')
print(f'It can only be described as {adjective} as it is seen {action} around the Verse.')

Required positional argument

Above I defined a new function and ‘max_speed’ is the first argument added. It is the only mandatory argument in the above function. A ‘max_speed’ must be given in order for the function to be called without error.

star_citizen_ship('1347 m/s')

Output:

The max speed of the Origin 350r is 1,347 m/s. It can only be described as shiny as it is seen zooming around the Verse.

def star_citizen_ship(max_speed, adjective='shiny',
action='zooming', model='Origin 350r'):

Default Arguments

The next three arguments adjective, action, and model come with default values. These arguments do not need to be passed to the function at all. If no value is given then the values given in the function definition above will be used. To override the default value we can call the function and assert a value after the keyword like so:

star_citizen_ship('1,345 m/s', adjective='sleek',
action='racing', model='Origin M50')

Output:

The max speed of the Origin M50 is 1,345 m/s. It can only be described as sleek as it is seen racing around the Verse.

Origin M50

Parameters can be made positional-only by adding a ‘/’ after them.

def star_citizen_ship(max_speed, mass, adjective='shiny',/,
action='zooming', model='Origin 350r'):

Above the parameters ‘max_speed’, ‘mass’ and ‘adjective’ are now positional-only.

Parameters can also be keyword-only by adding an * before them.

def star_citizen_ship(max_speed, mass, *, adjective='shiny',
action='zooming', model='Origin 350r'
):

‘adjective’, ‘action’, and ‘model’ are now keyword-only.

*Args and **Kwargs

A variable number of positional or keyword arguments can be provided. For example if you wanted to add crew members or how many hardpoints the ship has you could use *args. To add locations visited with the ship here I use **kwargs:

def star_citizen_ship(max_speed, mass, *, adjective='shiny',
action='zooming', model='Origin 350r', *hardpoints, **locations_visited):

The ‘*hardpoints’ var-positional will be passed into the function as a tuple and the ‘**locations_visited’ var-keyword arguments will be passed in as a dictionary.

Conclusion

Thank you for reading. If you enjoyed this article why not check out more of my content! Here are some guides and articles that are closely related:

  • Create a small executable program while learning about the terminal and environment variables! — Link
  • Windows Terminal a versatile open-source terminal emulator — Link
  • Recursive Functions in Python — Link
  • Preparing for a Python skills test? — Link
  • Using and calling an API with Python — Link
Approach to Microtech

Sources

  1. Difference between parameters and arguments — Python docs
  2. Parameters — Python Glossary
  3. Arguments — Python Glossary
  4. Iterable — Python Glossary

--

--

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.