Basic

Parameters and arguments

A parameter is a name a function needs each time it runs. An argument is the value you pass in for that name at the call.

What you will see

TypeError

About 1 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Pass values into a function.

1

Define a function with one parameter.

def greet(name): print("hi " + name)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
no output yet
2

Call it with an argument.

greet("Ada")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
hi Ada
3

Define one with two parameters and call it.

def add(a, b): return a + b print(add(3, 4))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
7
4

Call it with the wrong number of arguments.

print(add(3))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
TypeError
Every parameter needs one argument. Missing one is a call-time error, not a typo in the body.

Data Flow

Parameters are the input names a function needs, and arguments are the values passed in for them at each call.
1 def names the parameters
↓
2 call passes arguments
↓
3 arguments fill parameters
↓
4 the body uses the names
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def greet(name): sets nothing on screen
print("hi " + name) no output yet
Why this works
Why parameters matter

Without parameters a function can only do one fixed job. Parameters are the inputs that let the same steps work on new data each time.

When you have a choice

Parameter versus argument

A parameter is the placeholder written in the def. An argument is the real value supplied at the call. Every call must fill every parameter with one argument.

Examples

One parameter

def greet(name):
    print("hi " + name)

greet("Ada")

name is the parameter. The value "Ada" at the call is the argument, and inside the body name holds it.

Two parameters

def add(a, b):
    return a + b

print(add(3, 4))

Parameters line up left to right with the arguments. add(3, 4) puts 3 in a and 4 in b.

Different arguments each time

def square(n):
    return n * n

print(square(2))
print(square(9))

The same body runs with different values. The parameters are what change, which is what makes one function useful many times.

Wrong count fails

def add(a, b):
    return a + b

print(add(3))

Passing too few arguments fails. The function asked for two names and only got one, so it cannot start.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Running a function with a result
Previous Lesson
Running a function with a result
Next Lesson
Passing different kinds of values
Passing different kinds of values →