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.
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.
About 1 min of reading
Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.
Pass values into a function.
Define a function with one parameter.
Before you run it, say out loud what it will print. Then open the answer.
Call it with an argument.
Before you run it, say out loud what it will print. Then open the answer.
Define one with two parameters and call it.
Before you run it, say out loud what it will print. Then open the answer.
Call it with the wrong number of arguments.
Before you run it, say out loud what it will print. Then open the answer.
Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.
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.
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.
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.
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.
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.
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.
Each one is defined here on this site. Open the entry for the full definition and the other words it points at.