Course Progress32%
🍎 Python Basics Topic 32 / 100
⏳ 8 min read

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."

— ShurAI

Positional 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:

python
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:

python
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:

python
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!
Rule: defaults go at the end

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

python — coffee order system
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)
output
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

Positional
f("Riya", 22)
Matched by order.
Must get every one right.
Keyword
f(age=22, name="Riya")
Matched by name.
Order doesn't matter.
Default
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?