Python Function Parameters and Arguments, Explained Like Ordering Food
When you order a pizza, you don't just say "pizza." You say "large pizza, extra cheese, no olives." The restaurant has a base recipe, but your order customizes it. Python function parameters work the same way: they let you customize what a function does each time you call it.
Parameters vs. Arguments
These two words get mixed up constantly, so let's settle it here:
- A parameter is the placeholder in the function definition, like a blank spot on an order form.
- An argument is the actual value that you pass to a function when calling it.
def order_pizza(size): # "size" is the parameter print(f"Making a {sizef-string (Formatted String Literal)
An f-string in Python is a simple way to put the value of a variable directly inside a string.
-
Without an f-string
Pythonname = "John"print("Hello, " + name) # Hello, John -
With an f-string
Pythonname = "John"print(f"Hello, {name}") # Hello, John
Multiple Parameters
Just like a real order form has several fields, a function can take several parameters.
def order_pizza(size, topping): print(f"Making a {size} pizza with {topping}") order_pizza("large", "extra cheese")Python here matches the arguments of the parameters by position: "large" goes to size, "extra cheese" goes to topping.
Default Parameter Values
Most restaurants have a default topping if you don't specify any. Python lets you do this with default parameter values.
def order_pizza(size, topping="cheese"): print(f"Making a {size} pizza with {topping}") order_pizza("medium") # uses default: cheeseorder_pizza("medium", "pepperoni") # overrides defaultDefaults make a function flexible without forcing every caller to specify everything.
Keyword Arguments
Instead of relying on order, you can explicitly name each argument, much like filling in labeled fields on an order form instead of in a strict sequence.
Because each argument is labeled, the order doesn't matter. This is especially useful in functions with many parameters, where remembering the exact position of each one gets error-prone.
Mixing Positional and Keyword Arguments
You can combine both styles, but positional arguments must always come first.
- Positional arguments (e.g. "medium", "pepperoni") — Arguments must be given in the exact sequential order of the parameters.
- Keyword arguments (e.g. topping="cheeze", size="medium") — Once all positional inputs are satisfied, they can be placed in any order relative to each other.
Required vs. Optional Parameters
A parameter without a default value is required, Python will raise an error if you don't provide it.
Think of required parameters as fields that the restaurant can't take an order without, like size, and optional parameters as anything that has a valid default, like toppings.
Putting It Together
def order_pizza(size, topping="cheese", extra_cheese=False): order = f"{size} pizza with {topping}" if extra_cheese: order += " and extra cheese" return order print(order_pizza("large", "pepperoni", extra_cheese=True))Here:
if extra_cheese:checks whether the customer asked for extra cheese.- As we called the function with:
extra_cheese=True - So the contition is True and hence python runs
order += " and extra cheese" - Now the order becomes
large pizza with pepperoni and extra cheese - Finally,
returnsends the completed order back to whoever called the function.
This single function now handles a wide range of orders without needing a separate function for every combination.
Parameters are like options on an order form. Some options may have default values, while others can be customized when calling a function.
What's Next
Now you know how to customize a function's behavior per call. Next in this series: *args and **kwargs, when you don't know how many toppings someone wants to order.
Python Functions 7-Part Masterclass:
- Part 1: Python Functions Explained Like a Recipe (Beginner's Guide)
- Part 2: Python Function Parameters and Arguments (Explained Like Ordering Food) (Current)
- Part 3: *args and **kwargs in Python Explained Like a Potluck Party
- Part 4: Python Lambda Functions Explained Like a Sticky Note
- Part 5: Python Variable Scope Explained Like an Office Building
- Part 6: Python Recursion Explained Like Russian Nesting Dolls
- Part 7: Python Closures and Decorators Explained Like Gift Wrapping