Passing Arguments to Python Functions

Two cartoon robots smiling as one passes a yellow box labelled “age” to the other.

Functions are more powerful when you can give them information to work with. This is done through arguments. An argument is a value you pass to a function when you call it. Python then uses that value inside the function. Let’s look at how this works step by step.

Positional arguments

The simplest way to pass arguments is in the order they are defined. These are called positional arguments.

Here, “LiddleBit” is matched to name and 25 is matched to age. The order matters.

Keyword arguments

You can also pass arguments using the parameter names directly. These are called keyword arguments.

This works even though the order is different, because each value is linked to a name. Using keyword arguments can greatly improve the readability of your code, especially if a function has several parameters. It makes it obvious what each argument means, rather than relying on the reader to remember the order.

Default values

Sometimes you want a parameter to have a default value. This means you can call the function without providing that argument, and Python will use the default instead.

Defaults make your functions more flexible, as they work with or without all arguments provided.

Mixing it all together

You can combine positional, keyword, and default arguments, but keep in mind that positional arguments must come first.

Things to try yourself

  1. Write a function called favourite_colour that takes two arguments: name and colour. Give colour a default value.
  2. Write a function called area that takes length and width and prints the area. Try calling it with both positional and keyword arguments.
  3. Write a function called describe_pet with parameters animal_type (default “dog”) and pet_name. Call it with different combinations of arguments.

By learning how to use positional arguments, keyword arguments, and default values, you make your functions easier to use and more adaptable. Keyword arguments in particular help your code stay clear and self-explanatory, which is invaluable when projects grow or when other people read your work.

Main Topic

Python Functions

A flat, landscape-style digital illustration shows a man in an orange shirt drinking from a glass of water. A speech bubble next to him contains the function call drink_water() in bold monospaced text.

A beginner-friendly post explaining Python functions, highlighting clarity, reusability, collaboration, and scalability benefits.

Other Tutorials in this Topic

Illustration showing Python function syntax with def, a function name, parentheses, and indented placeholder code.

Defining Python Functions

Beginner-friendly post introducing Python functions, explaining their syntax, naming conventions, and usage, with simple examples and practice…

Three smiling robots on a blue background, each smaller than the last, passing a yellow box with speech bubble showing process().

Recursion in Python

Beginner-friendly Python guide introducing recursion, explaining base cases, benefits, and examples like countdown and string reversal, with…

A smiling blue robot opens a red toolbox against a pale green background, holding a silver spanner labelled “lambda” in black.

How to use python Lambda Functions

Beginner-friendly tutorial explaining Python lambda functions, covering syntax, simple examples, practical uses with map, filter, sorted, and…