Positional vs Named Parameters
You'll learn how parameters can be passed to functions by their position or by their name, and why that matters.
When you call a function, you need to supply the right information in the right way. There are two main approaches: positional and named parameter passing.
Positional Parameters
With positional parameters, the order matters. The function matches arguments to parameters based on their position in the list. Think of it like filling out a form where you write your first name in box 1, last name in box 2, and age in box 3 — the position tells the computer what each value means.
def greet(name, age):
# name gets first argument, age gets second
return "Hello " + name + ", you are " + str(age)
greet("Alice", 25) # "Alice" → name, 25 → age
If you swap the order, you get nonsense: greet(25, "Alice") would try to make the name equal to 25!
Named Parameters
With named parameters (also called keyword arguments), you explicitly state which parameter gets which value. Now order doesn't matter because you're labeling each piece of data.
greet(age=25, name="Alice") # works perfectly!
greet(name="Bob", age=30) # also works
This is like filling out a form where each box is labeled — you can fill them in any order because the labels tell the computer what goes where.
When to Use Each
Use positional when the function has few parameters and the order is obvious. Use named when there are many parameters, when order is confusing, or when you want to make your code more readable and self-documenting.
Key Takeaway: Positional parameters rely on order, while named parameters use explicit labels — choose positional for simplicity and named for clarity.