Function Parameters
Positional, keyword, and default parameters — three ways to pass data into a function, each with its own strengths.
"Parameters are the inputs to your function. Python gives you three ways to send them — by position, by name, or with a built-in default."
— ShurAIPositional Parameters — Order Matters
The most common type. Values are matched to parameters by their position — first value goes to first parameter, second to second, and so on:
def describe_pet(name, animal, age):
print(f"{name} is a {age}-year-old {animal}.")
# Arguments matched by position
describe_pet("Buddy", "dog", 3)
# Buddy is a 3-year-old dog.
# Wrong order = wrong output!
describe_pet(3, "Buddy", "dog")
# 3 is a dog-year-old Buddy. <— oops!
Keyword Arguments — Use the Name
Pass values by writing parameter_name=value. Order no longer matters — much safer for functions with many parameters:
def book_flight(origin, destination, passengers):
print(f"Booking: {origin} → {destination} ({passengers} pax)")
# Keyword arguments — order does not matter
book_flight(destination="Goa", passengers=2, origin="Mumbai")
# Booking: Mumbai → Goa (2 pax)
# Mix positional and keyword (positional must come first)
book_flight("Delhi", passengers=1, destination="Chennai")
# Booking: Delhi → Chennai (1 pax)
Default Parameters — Built-in Fallback
Give a parameter a default value using = in the function definition. If the caller does not provide that argument, the default is used automatically:
def send_message(to, message, priority="normal"):
print(f"[{priority.upper()}] To: {to} | {message}")
# Without priority — uses default "normal"
send_message("Riya", "Your order has shipped!")
# [NORMAL] To: Riya | Your order has shipped!
# With priority — overrides default
send_message("Arjun", "System outage!", "urgent")
# [URGENT] To: Arjun | System outage!
Parameters with default values must always come after parameters without defaults. def f(a, b=5) is fine. def f(a=5, b) raises a SyntaxError.
All Three Together — Real Example
def order_coffee(name, size, milk="whole", sugar=1):
"""Place a coffee order with optional customisation."""
print(f"Order for {name}:")
print(f" {size.title()} coffee")
print(f" Milk: {milk}")
print(f" Sugar: {sugar} tsp")
print()
# Positional only
order_coffee("Riya", "large")
# Positional + keyword default override
order_coffee("Arjun", "medium", milk="oat", sugar=0)
# All keyword arguments
order_coffee(size="small", name="Sneha", sugar=2)
Order for Riya:
Large coffee
Milk: whole
Sugar: 1 tsp
Order for Arjun:
Medium coffee
Milk: oat
Sugar: 0 tsp
Order for Sneha:
Small coffee
Milk: whole
Sugar: 2 tsp
Quick Visual Summary
f("Riya", 22)Matched by order.
Must get every one right.
f(age=22, name="Riya")Matched by name.
Order doesn't matter.
def f(name, age=18)Fallback value.
Caller can skip it.
"Default parameters are like pre-filled forms — most people leave them as-is, but anyone who needs something different can change them."
— ShurAI🧠 Quiz — Q1
In greet("Riya", 22), how are the arguments matched?
🧠 Quiz — Q2
What is a keyword argument?
🧠 Quiz — Q3
Given def send(to, msg, priority="normal") — what happens if you call send("Riya", "Hi")?
🧠 Quiz — Q4
Which function definition has a syntax error?