Parameters and defaults
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.

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:
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)) # 650Let's walk through the calls line by line:
total(250)passes only the price.counttakes1,discounttakes0, and the function returns250.total(250, 3)turns the first knob:countis3, and the discount is still0. The result is750.total(250, 3, 100)sets all three parameters and returns650.
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:
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:
print(total(250, discount=50)) # 200
print(total(discount=50, price=250)) # 200
print(total(250, count=2, discount=100)) # 400What 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)givesTypeError: total() got multiple values for argument 'price'; - the name must match a parameter name: the typo in
total(250, cnt=2)givesTypeError: 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:
rate = 10
def with_rate(x=rate):
return x
rate = 20
print(with_rate()) # 10
print(with_rate(5)) # 5Step by step:
- The
defline runs whilerateis10. The function remembers the value10forx. rate = 20changes the variable, but the remembered value stays the same.with_rate()prints10, andwith_rate(5)prints the passed5.
Common mistake
A keyword argument is placed before a positional one:
print(total(price=250, 3)) # ✗SyntaxError: positional argument follows keyword argumentArgument 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
defascount=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
defline runs.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.