Give one reusable action the input it needs.
Concept 15 showed a function that always did the same thing. Parameters solve the next problem: one function can perform the same action with different values supplied at each call.
def greet(name):
print("Hello,", name)
greet("Ana")
greet("Marko")
greet("Jelena")A parameter is the name inside a function definition for an incoming value. In def greet(name):, name is a parameter. An argument is the actual value supplied at a call. In greet("Ana"), "Ana" is an argument. One function can therefore greet different people without copying the definition.
Use this beginner heuristic: definition → parameter; call → argument. At greet("Ana"), "Ana" is passed to name; during that call, name refers to "Ana", then the body runs using that value.
One action, different input: The next call can use a different argument. At greet("Marko"), name refers to "Marko" for that call. The function definition stays the same while its result can change with the input.
def greet(name):
print("Hello,", name)
greet("Ana")Python reaches greet("Ana"), passes "Ana" to the parameter name, and runs the body. During this call, name represents "Ana", so the output is Hello, Ana. When the call ends, the function is ready to be called again with another argument.
def introduce(name, city):
print(name, "lives in", city)
introduce("Ana", "Novi Sad")A function can need more than one incoming value. In this positional call, name receives "Ana" and city receives "Novi Sad". Arguments map to parameters by their positions.
introduce("Novi Sad", "Ana")This is valid Python, but it gives the parameters the wrong meanings: name would receive "Novi Sad" and city would receive "Ana". Valid syntax is not always the intended result.
def double(number):
print(number * 2)
double(5)
double(10)An argument does not have to be text. Here number receives a number for each call, and the same function body uses that received value to produce 10 and 20.
introduce("Ana", "Novi Sad")
# name ← "Ana"
# city ← "Novi Sad"Before tracing the body, connect every argument in the call to the parameter in the matching position. Then imagine the values those parameters represent for this call.
A practical parameter-reading checklist
- Find the parametersLook inside the parentheses of the function definition for the names that receive input.
- Find the callLook inside the parentheses of the call for the actual arguments supplied.
- Map arguments to parametersFor an ordinary positional call, connect the first argument to the first parameter, then continue in order.
- Imagine the received valuesDecide what each parameter represents during this one call.
- Then trace the bodyRead the function body only after you know which values its parameters have.
Common beginner mistakes
- Mixing up parameter and argumentA parameter appears in the definition; an argument is the concrete value supplied in a call.
- Calling without a required argumentIf
greetneedsname, thengreet()has no value to pass into that parameter. - Passing too many argumentsA call must supply the number of values the current function definition expects.
- Using the wrong positional orderWith several positional arguments, Python maps values by order. Check that each value belongs with that parameter.
- Expecting a parameter to keep a previous call’s valueEach call supplies its own arguments. Trace the values for the current call instead of carrying a value over from the last one.
- Hard-coding one value inside the function bodyWriting one concrete name inside the body makes the function less flexible. Use the parameter when the action should work with different input.
