Skip to content

Intermediate · Functions · topic 19 of 50 · 20 min

Parameters, arguments and defaults

Explanation

Parameters are the names in the def line; arguments are the values you pass when you call. You can pass arguments by position or by name (keyword arguments), and give parameters default values. Never use a list as a default value: it is shared by every call.

Parameters and arguments

A parameter is a name written in the brackets of def. It is a placeholder for a value. An argument is the actual value you pass when you call the function. In def add(a, b), a and b are parameters. In add(2, 3), 2 and 3 are arguments.

Think of it like this: Think of a railway reservation form. The form has blanks: Name, Age, From, To. Those blanks are the parameters. The details you write in, like "Asha, 21, Pune, Delhi", are the arguments. The same form works for every passenger.

Positional arguments

python
def describe(name, city):
    return name + " lives in " + city

print(describe("Asha", "Pune"))
print(describe("Pune", "Asha"))
Positional arguments are matched by position: first to first, second to second. Swapping them gives a silly answer.

Keyword arguments

python
def describe(name, city):
    return name + " lives in " + city

print(describe(city="Pune", name="Asha"))
print(describe("Asha", city="Pune"))
A keyword argument names the parameter, so the order does not matter. Both lines show Asha lives in Pune.

You can mix the two kinds, but positional arguments must come first. describe("Asha", city="Pune") is fine, while describe(name="Asha", "Pune") is a SyntaxError.

Default values

python
def ticket_price(base, quota="general"):
    if quota == "tatkal":
        return base + 200
    return base

print(ticket_price(500))
print(ticket_price(500, "tatkal"))
quota has a default value, "general", which is used when you leave it out. Shows 500, then 700.
output
def ticket_price(quota="general", base):
    return base
SyntaxError: parameters with a default must come after the ones without a default.

The mutable default trap

A default value is made only ONCE, when Python reads the def line, not each time you call the function. For numbers and strings this does not matter. But a default list is shared by every call, so items pile up from one call to the next.

python
def add_item(item, cart=[]):
    cart.append(item)
    return cart

print(add_item("rice"))
print(add_item("dal"))
Surprise! The second call shows ['rice', 'dal'], because both calls share the same default list.
python
def add_item(item, cart=None):
    if cart is None:
        cart = []
    cart.append(item)
    return cart

print(add_item("rice"))
print(add_item("dal"))
The fix: use None as the default and make a new list inside the function. Shows ['rice'], then ['dal'].
Careful: Never use a list or a dict as a default value, as in def f(items=[]). Write items=None instead, and inside the function write if items is None: items = []. The check is None asks whether a value is exactly None.

Returning several values

python
def split_bill(total, people):
    each = total // people
    left = total % people
    return each, left

each, left = split_bill(1000, 3)
print("Each pays", each, "and", left, "rupee is left")
Return several values separated by commas and Python packs them into a tuple. Unpack them where you call. Shows Each pays 333 and 1 rupee is left.
Tip: Use keyword arguments when a function has many parameters or True/False options. send_sms(number, text, urgent=True) is much clearer than send_sms(number, text, True).

Watch the lesson

Parameters, arguments and defaults · 1/7

Parameters and arguments

In this lesson you will learn how to pass values into functions in different ways.

Prefer a video by a teacher? Find free videos on “python function arguments default keyword arguments tutorial” on YouTube ↗

In real life: Splitting a restaurant bill

After dinner, friends want to split the bill. Usually there are two of them and no tip, so those are the defaults. On a birthday there are five people and a 10% tip, and keyword arguments make that call easy to read.

python
def share(total, people=2, tip_percent=0):
    with_tip = total + total * tip_percent / 100
    return round(with_tip / people, 2)

print(share(800))
print(share(2500, people=5, tip_percent=10))
print(share(1200, 3))
Shows 400.0, then 550.0, then 400.0.

Practice (0/3 done)

Task 1 · Code · Python

Write a function greet(name, greeting="Namaste") that returns the greeting, a comma, a space, the name and an exclamation mark. Example: greet("Asha") returns "Namaste, Asha!" and greet("Ravi", "Hello") returns "Hello, Ravi!".

Task 2 · Code · Python

This add_item function has the mutable default bug: calls without a cart share one list. Fix it so that each call without a cart starts with a NEW empty list. Calls that pass a cart should still add to that cart.

Task 3 · Code · Python

Write a function bill_split(total, people=2, tip_percent=0) that adds the tip to the total, divides by the number of people, and returns the share rounded to 2 decimal places. Example: bill_split(1000, 4, 10) returns 275.0.

Quick quiz

Question 1

1.In def add(a, b): and the call add(2, 3), what are 2 and 3?

Question 2

2.def power(n, p=2): return n ** p. What is power(3)?

Question 3

3.def minus(a, b): return a - b. What is minus(b=10, a=3)?

Question 4

4.Which def line is a SyntaxError?

Question 5

5.Why is def f(items=[]) a problem?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Defining and calling functions