Intermediate · Functions · topic 19 of 50 · 20 min
Parameters, arguments and defaults
Explanation
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.
Positional arguments
def describe(name, city):
return name + " lives in " + city
print(describe("Asha", "Pune"))
print(describe("Pune", "Asha"))Keyword arguments
def describe(name, city):
return name + " lives in " + city
print(describe(city="Pune", name="Asha"))
print(describe("Asha", city="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
def ticket_price(base, quota="general"):
if quota == "tatkal":
return base + 200
return base
print(ticket_price(500))
print(ticket_price(500, "tatkal"))def ticket_price(quota="general", base):
return baseThe 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.
def add_item(item, cart=[]):
cart.append(item)
return cart
print(add_item("rice"))
print(add_item("dal"))def add_item(item, cart=None):
if cart is None:
cart = []
cart.append(item)
return cart
print(add_item("rice"))
print(add_item("dal"))Returning several values
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")Watch the lesson
Parameters, arguments and defaults · 1/7
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.
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))Practice (0/3 done)
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!".
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.
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
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.