Function Arguments in Python: parameters make code flexible
A function with no inputs does one fixed thing. Add parameters and it does a whole family of things — spawn_wave(5, 88) and spawn_wave(10, 44) from one definition.
The big idea
Parameters are the named inputs in a function's definition; arguments are the actual values you pass when you call it.
See it in code
A parameter is a named input in the definition; an argument is the value you pass at the call. report declares one parameter, count, and each call supplies a different argument:
def report(count):
print("Wave of", count)
report(5)
report(10)Wave of 5 Wave of 10
count is the parameter; 5 and 10 are the arguments. One definition already does two jobs.
Add a second parameter and the function computes with both. wave_positions(count, spacing) loops count times and turns each index into an evenly spaced x — the exact formula we're about to spawn a fleet with:
def wave_positions(count, spacing):
for i in range(count):
print(55 + i * spacing)
wave_positions(3, 88)55 143 231
55 + i * spacing maps 0, 1, 2 to 55, 143, 231. Change spacing and the whole row tightens or spreads — from one argument.
Now feed those positions to real enemies. spawn_wave takes the same two parameters — count and spacing — and builds an Enemy at each computed x. Different arguments, different wave, no rewriting:
from game import Stage, Enemy
screen = Stage.create()
Stage.clear(screen)
def spawn_wave(count, spacing):
return [Enemy(55 + i * spacing, 90) for i in range(count)]
for enemy in spawn_wave(5, 88):
enemy.draw(screen)
count and spacing are the parameters; 5 and 88 are the arguments. Call spawn_wave(10, 44) and the same function tightens ten enemies into the row. One definition, endless waves.
Parameterizing is how a function earns reuse: moving_average(prices, window), roll(sides), resize(image, width, height). Add a default — def spawn_wave(count, spacing=74) — and callers can skip the argument when the common value is fine.
Try it yourself
Give spacing a default of 74, then call spawn_wave(6) with no second argument. Next, call it with keyword arguments — spawn_wave(count=6, spacing=60) — which makes the call self-documenting.
The common mistake
Mixing up parameters and arguments, and their order. spawn_wave(88, 5) is legal but wrong — it reads 88 as the count, trying to spawn 88 enemies with spacing 5. Positional arguments are matched by position, so order is everything (unless you name them).
What it unlocks
Flexible inputs pair with return values to build reusable tools, and lead into scope — how a parameter lives only inside its function.