Parameters are how Python functions receive outside information. Without them, every function would produce the same result every time it ran. With them, a single function can handle countless different inputs. This tutorial walks through the decision process beginners face: when does a function actually need a parameter, what kind of parameter fits, and how do you write and call it correctly?
A Python function is a reusable block of code. When you define one, you decide whether it needs external data to do its job. Parameters are the mechanism for passing that data in. Understanding when to use them — and which kind to use — is one of the first real decisions beginners make on the way to writing clean, flexible code.
What a Parameter Actually Does
When Python sees a function definition, it reads the name, then looks inside the parentheses. Anything listed there is a parameter — a named slot that will hold a value when the function is called. Inside the function body, that name behaves like any other variable. The difference is that its value comes from the caller, not from an assignment inside the function.
Compare these two functions:
# No parameter — always greets the same person
def greet_fixed():
print("Hello, Alice!")
greet_fixed() # Hello, Alice!
greet_fixed() # Hello, Alice! — can never change
# With a parameter — works for any name
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Hello, Alice!
greet("Bob") # Hello, Bob!
greet("Carlos") # Hello, Carlos!
The parameter name is the variable defined in the function signature. The values "Alice", "Bob", and "Carlos" passed during each call are called arguments. The parameter name stays the same; the argument value changes every time.
The terms are related but distinct. A parameter is the variable name in the function definition: def greet(name). An argument is the value passed when calling the function: greet("Alice"). You define parameters once. You supply new arguments every time you call the function.
Build a function that takes a single parameter called city and prints a message using it. Click each token in the correct order:
def, followed by the function name and an opening parenthesis. The parameter name goes inside the parentheses, then a closing parenthesis and colon. The indented body uses the parameter like any variable. return city would return a value but not print anything, and city = 'Paris' would ignore the parameter entirely.
When to Add a Parameter
The question is not just how to write a parameter — it is when writing one makes sense. The test is straightforward: if a value inside the function will change depending on who calls it or what data they have, that value belongs as a parameter.
There are three practical situations where a parameter is the right tool:
- The function needs data that the caller possesses but the function cannot know in advance, such as a username, a file path, or a number to calculate with.
- The function should behave differently depending on context — for example, formatting currency for different countries.
- The function is reused across many parts of a program, and hardcoding a value inside it would make it only work for one specific case.
Here is a concrete example. Suppose you need to calculate the area of a rectangle. If you write the dimensions directly into the function, it only works for one specific rectangle. Adding parameters makes it work for any rectangle:
# Rigid — only calculates area for a 5x3 rectangle
def area_fixed():
return 5 * 3
# Flexible — works for any rectangle
def area(width, height):
return width * height
print(area(5, 3)) # 15
print(area(10, 7)) # 70
print(area(2.5, 4)) # 10.0
If you find yourself writing nearly identical functions that differ only in one hardcoded value, that value is almost certainly a parameter waiting to be extracted. Functions are tools for reuse — parameters are what make reuse possible.
The table below compares scenarios where a parameter is the right choice against scenarios where it is not needed. Click each row to see the reasoning:
- Use a parameter?
- Yes — the price changes with each call. Define
def calculate_tax(price, rate). - Why
- Without a parameter, the function could only calculate tax for one fixed price. Any other price would require a new function.
- Use a parameter?
- No — the notice text does not change. No parameter needed.
- Why
- If the output is always identical regardless of who calls the function, external input serves no purpose. The text can live directly in the function body.
- Use a parameter?
- Yes — the recipient changes every time. Define
def send_welcome(username, email). - Why
- Each new user has a different name and address. Those values must come from the caller so the function can personalize the message correctly.
- Use a parameter?
- Yes — the Celsius value is provided by the caller. Define
def to_fahrenheit(celsius). - Why
- The conversion formula is fixed, but the input temperature varies. The parameter carries the temperature in; the formula stays inside the function.
This function is supposed to return the full name of a user by combining first and last name. One line contains the bug. Click it, then hit check.
full_name("Ada") to full_name("Ada", "Lovelace") or any valid last name. The function full_name defines two required parameters — first and last. Calling it with only one argument causes a TypeError because last has no default value and receives nothing. Required parameters always need a matching argument.
Types of Parameters You Will Encounter
Python functions support several kinds of parameters. As a beginner, the four you will use regularly are required parameters, default parameters, positional arguments, and keyword arguments. Understanding the difference between them helps you write function calls that are both correct and readable.
Required Parameters
A required parameter has no default value. Every call to the function must supply an argument for it, or Python raises a TypeError. Required parameters represent data that the function cannot operate without.
def divide(numerator, denominator):
return numerator / denominator
print(divide(10, 2)) # 5.0
# divide(10) # TypeError — denominator is required
Default Parameters
A default parameter has a fallback value assigned in the definition using =. If the caller does not pass a value for that parameter, Python uses the default. Default parameters must always come after required parameters in the function signature.
def power(base, exponent=2):
return base ** exponent
print(power(3)) # 9 — uses default exponent of 2
print(power(3, 3)) # 27 — caller overrides the default
print(power(2, 10)) # 1024
Python enforces a strict ordering: all required parameters must come before any default parameters. Writing def func(x=1, y) causes a SyntaxError. Required first, defaults after — always.
Positional vs. Keyword Arguments
These describe how you pass arguments when calling a function, not how the function defines them. A positional argument relies on order — Python matches the first argument to the first parameter, the second to the second, and so on. A keyword argument uses the parameter name explicitly, which means you can pass them in any order.
def describe_pet(animal, name):
print(f"{name} is a {animal}.")
# Positional — order matters
describe_pet("dog", "Rex") # Rex is a dog.
describe_pet("Rex", "dog") # dog is a Rex. (wrong order!)
# Keyword — order does not matter
describe_pet(name="Rex", animal="dog") # Rex is a dog.
describe_pet(animal="dog", name="Rex") # Rex is a dog.
Keyword arguments are particularly useful when calling a function that has several parameters. Naming each argument in the call removes any ambiguity about which value maps to which parameter.
"A function call with keyword arguments is self-documenting." — Python community convention
How to Add Parameters to a Python Function
Follow this four-step process each time you define a function and need to decide whether it requires parameters and what kind to use.
-
Decide what information the function needs from the caller
Ask whether the value changes between calls. If a piece of data belongs to the caller and not to the function itself, it should be a parameter. Write that list of inputs before writing any code.
-
Write the function signature with the parameter name
Place the parameter name inside the parentheses after
defand the function name. Choose a descriptive name that reflects the expected data —username,file_path,temperature. Avoid single-letter names except in short mathematical functions. -
Determine whether the parameter needs a default value
If the parameter is optional or has a sensible fallback, assign a default with
=in the definition. Place all required parameters before any default parameters, or Python will raise aSyntaxError. -
Call the function with matching arguments
Pass values that match each parameter either positionally (matching by order) or using keyword syntax (matching by name). Verify that all required parameters receive a value and that the values make sense for what the function expects.
Python Learning Summary Points
- A parameter is a named variable in the function definition that holds a value supplied by the caller. Add one whenever the function needs data that changes between calls.
- Required parameters must always receive an argument. Default parameters provide a fallback value and can be omitted by the caller. Required parameters must appear before default parameters in the signature.
- Arguments can be passed positionally (by order) or as keyword arguments (by name). Keyword arguments improve readability and remove ambiguity when a function has multiple parameters.
Parameters transform functions from fixed scripts into reusable, adaptable tools. The next step after mastering the four types covered here is exploring *args and **kwargs, which let a function accept a variable number of positional and keyword arguments — but that builds directly on what you have learned in this tutorial.
Frequently Asked Questions
A parameter is a named variable listed inside the parentheses in a function definition. It acts as a placeholder for the value that will be passed into the function when it is called. Parameters let the same function work with different input data each time it runs.
A parameter is the variable name defined in the function signature, such as name in def greet(name). An argument is the actual value passed in when calling the function, such as "Alice" in greet("Alice"). Parameters are defined once; arguments change with each call.
Add a parameter whenever the function needs outside information to do its job, and that information may differ between calls. If a value inside the function will always be the same no matter how it is called, it does not need to be a parameter.
A default parameter value is a fallback value assigned to a parameter in the function definition using an equals sign, such as def greet(name='stranger'). If the caller does not pass a value for that parameter, Python uses the default. Default parameters must always be placed after any required parameters.
A keyword argument is an argument passed to a function using the parameter name explicitly, such as greet(name='Alice'). Keyword arguments can be written in any order and make function calls easier to read, especially when a function has several parameters.
A positional argument is an argument passed to a function based on its position in the call, without using the parameter name. Python matches each positional argument to the corresponding parameter from left to right. The order of positional arguments must match the order of parameters in the definition.
Yes. A function can have a mix of required parameters and default parameters. Required parameters must appear first in the function signature, and default parameters must follow. Placing a required parameter after a default parameter causes a SyntaxError.
Python raises a TypeError indicating that the function is missing a required positional argument. Required parameters must always be provided by the caller because they have no fallback default value.
Python does not impose a hard limit on the number of parameters a function can have. However, functions with a large number of parameters can be difficult to read and call correctly. As a practical guideline, keeping functions focused and limiting parameters to what is genuinely needed makes code easier to understand and maintain.
Required parameters have no default value and must be provided in every function call. Optional parameters have a default value assigned in the definition and can be omitted when calling the function, in which case the default is used automatically.