Skip to content
Theory · Functions · lesson 1 of 8

Parameters and defaults

What you'll learn

What you'll learn:

  • give parameters default values and call a function with a different number of arguments;
  • pass arguments by position and by name;
  • tell local variables from global ones and change a global one with global;
  • write recursive functions and see how the calls wait for each other.
A coffee machine total takes a coin, while its two knobs are already set to count=1 and discount=0

Picture a coffee machine labeled total. You drop a coin into the slot: the price of one cup. Next to it are two knobs, "how many cups" and "discount". They are already set to count=1 and discount=0, and usually nobody touches them.

Want three cups? You turn one knob and leave the discount as it is. Without a coin the machine does nothing. Without turning the knobs it works fine: it takes whatever they are already set to.

Python functions work the same way. As you already know, the names in the def line are parameters, and the values in a call are arguments. The total function computes the cost of a purchase: the price times the count, minus a discount. Without presets, every call would pass three numbers, even for one cup with no discount:

Python
def total(price, count=1, discount=0):
    return price * count - discount


print(total(250))  # 250
print(total(250, 3))  # 750
print(total(250, 3, 100))  # 650

Let's walk through the calls line by line:

  1. total(250) passes only the price. count takes 1, discount takes 0, and the function returns 250.
  2. total(250, 3) turns the first knob: count is 3, and the discount is still 0. The result is 750.
  3. total(250, 3, 100) sets all three parameters and returns 650.

Default arguments

Writing count=1 in the def line sets a default argument: if the call passes no argument for count, the parameter gets 1. That is the preset knob.

A parameter without such a value is required: it is the coin. The call total() with no price stops with an error:

TEXT
TypeError: total() missing 1 required positional argument: 'price'

You have already met default arguments in print. Its parameters sep and end have values: a space and a move to a new line. That is why print without them separates values with a space and ends the line.

In the def line, parameters with a default value come after the required ones. The line def total(price=0, count): keeps the file from running: SyntaxError: parameter without a default follows parameter with a default.

Two ways to pass an argument

An argument can be passed by its place in the list or by the parameter's name. Let's look at both.

Positional arguments

Arguments without a name are called positional: they fill the parameters from left to right. In the call total(250, 3), 250 goes to price, 3 goes to count, and discount stays 0.

You cannot skip the middle parameter this way. The second positional argument always goes to the second parameter. An extra argument fails too: the call total(250, 3, 100, 5) gives TypeError: total() takes from 1 to 3 positional arguments but 4 were given.

Keyword arguments

To pass a discount and keep the default count, you name the argument after the parameter. This is a keyword argument, like sep="-" in a call to print:

Python
print(total(250, discount=50))  # 200
print(total(discount=50, price=250))  # 200
print(total(250, count=2, discount=100))  # 400

What matters about keyword arguments:

  • their order does not matter: the second line gives the same as the first;
  • positional arguments come first, keyword arguments after them;
  • one parameter cannot get two values: total(250, price=300) gives TypeError: total() got multiple values for argument 'price';
  • the name must match a parameter name: the typo in total(250, cnt=2) gives TypeError: total() got an unexpected keyword argument 'cnt'.

This is where the coffee machine picture stops working. On a machine you can turn the knobs in any order. Positional arguments keep a strict order, and only a name lets you reach the far knob without touching the near one.

When a default value is computed

Interviewers like to ask this. A default value is computed once, when Python runs the def line. From then on the function keeps the ready value, not the expression:

Python
rate = 10


def with_rate(x=rate):
    return x


rate = 20
print(with_rate())  # 10
print(with_rate(5))  # 5

Step by step:

  1. The def line runs while rate is 10. The function remembers the value 10 for x.
  2. rate = 20 changes the variable, but the remembered value stays the same.
  3. with_rate() prints 10, and with_rate(5) prints the passed 5.

Common mistake

A keyword argument is placed before a positional one:

Python
print(total(price=250, 3))  # ✗
TEXT
SyntaxError: positional argument follows keyword argument

Argument order is checked along with the whole file before it runs, so not a single line runs. Write positional arguments first: total(250, 3) or total(250, count=3).

Key points

  • A default argument is set in def as count=1; such parameters come after the required ones.
  • Positional arguments fill parameters from left to right; keyword arguments go by name, in any order.
  • In a call, positional arguments come before keyword ones, or the file does not run.
  • A default value is computed once, when the def line runs.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way