Learning LibraryCore Coding LibraryTeens

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

1The basics

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:

python
def report(count):
    print("Wave of", count)

report(5)
report(10)
Run it — one definition, two different arguments:
Wave of 5
Wave of 10

count is the parameter; 5 and 10 are the arguments. One definition already does two jobs.

2A step further

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:

python
def wave_positions(count, spacing):
    for i in range(count):
        print(55 + i * spacing)

wave_positions(3, 88)
Run it — three indexes become three positions, 88 apart:
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.

3In our world

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:

python
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)
Run it — five enemies, spaced 88 pixels apart:
A row of five purple enemy sprites evenly spaced near the top of a dark play area.

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.

The same idea, everywhere

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.

Want the simpler version? Read the Kids version →