Parameters make functions more powerful because they enable the function to operate with slight variation by passing in pieces of information that the function needs in order to do its work. These values, often called arguments or actual parameters or parameter values, are passed to the function by the programmer. It is important to note that the programmer who wrote the function may be a different person than the programmer who uses the function. Here is a representation of a function that takes in information through parameters:
This type of diagram is often called a black-box diagram because it only states the requirements from the perspective of the user (well, the programmer, but the programmer who uses the function, who may be different than the programmer who created the function). The user must know the name of the function and what arguments need to be passed. The details of how the function works are hidden inside the “black-box”.
You have already been making function invocations with parameters. For example, when you write len("abc") or len([3, 9, "hello"]), len is the name of a function, and the value that you put inside the parentheses, the string “abc” or the list [3, 9, “hello”], is a parameter value.
When a function has one or more parameters, the names of the parameters appear in the function definition, and the values to assign to those parameters appear inside the parentheses of the function invocation when you call that function. Let’s look at each of those a little more carefully.
In the definition, the parameter list is sometimes referred to as the formal parameters or parameter names. These names can be any valid variable name. If there is more than one, they are separated by commas.
In the function invocation (call), inside the parentheses one value should be provided for each of the parameter names. These values are separated by commas. The values can be specified either directly, or by any python expression including a reference to some other variable name.
That can get kind of confusing, so let’s start by looking at a function with just one parameter. The revised hello function personalizes the greeting: the person to greet is specified by the parameter.
First, notice that hello2 has one formal parameter, s. You can tell that because there is exactly one variable name inside the parentheses on line 1.
Next, notice what happened during Step 2. Control was passed to the function, just like we saw before. But in addition, the variable ’s’ was bound to a value, the string “Iman”. When it got to Step 7, for the second invocation of the function, ’s’ was bound to “Jackie”.
Function invocations always work that way. The expression inside the parentheses on the line that invokes the function is evaluated before control is passed to the function. The value is assigned to the corresponding formal parameter. Then, when the code block inside the function is executing, it can refer to that formal parameter and get its value, the value that was ‘passed into’ the function.
Checkpoint8.2.1.
def hello2(s):
print("Hello " + s)
print("Glad to meet you")
hello2("Nick")
To get a feel for that, let’s invoke hello2 using some more complicated expressions. Try some of your own, too.
Now let’s consider a function with two parameters. This version of hello takes a parameter that controls how many times the greeting will be printed.
At Step 3 of the execution, in the first invocation of hello3, notice that the variable s is bound to the value “Wei” and the variable n is bound to the value 4.
That’s how function invocations always work. Each of the expressions, separated by commas, that are inside the parentheses are evaluated to produce values. Then those values are matched up positionally with the formal parameters. The first parameter name is bound to the first value provided. The second parameter name is bound to the second value provided. And so on.
Subsection8.2.2Parameter Order and Type
The order of the parameters matters, and so when you are calling a function with multiple parameters, you need to make sure that you specify the values in the correct order, and that you are passing in the right type of value for each parameter. Let’s return to the turtle example and add two more parameters to the square function, to specify the line width and color.
Note that now we set the color as part of the call to the draw_square method, and we are drawing the flower petal outlines with different thicknesses. The order of the values in the call match the order of the parameters on line 3.
Let’s examine the types of errors that we encounter if we mess up specifying the parameters. In the example below, we accidentally leave off the color parameter on line 24, and we get a TypeError noting that we are missing 1 required argument.
In this next example, we swap the order of the middle two parameters on line 24 (side length and line thickness). In this case we get a logic error, the program executes completely, but the output is not what we expect. Instead of a bunch of thin blue squares forming the inner flower petals, we get what looks like a blue circle being drawn over and over again, because we are drawing a square with sides of length 2 and a pen width of 50!
It is also possible to have optional parameters, so that if a programmer leaves off a parameter value when calling a function, the code still runs, but we won’t cover that in this course.
Check your understanding
Checkpoint8.2.2.
Which of the following is a valid function header (first line of a function definition)?
def greet(t):
A function may take zero or more parameters. In this case it has one.
def greet:
A function needs to specify its parameters in its header. If there are no paramters, put () after the function name.
greet(t, n):
A function definition needs to include the keyword def.
def greet(t, n)
A function definition header must end in a colon (:).
Checkpoint8.2.3.
What is the name of the following function?
def print_many(x, y):
"""Print out string x, y times."""
for i in range(y):
print(x)
def print_many(x, y):
This line is the complete function header (except for the semi-colon) which includes the name as well as several other components.
print_many
Yes, the name of the function is given after the keyword def and before the list of parameters.
print_many(x, y)
This includes the function name and its parameters
Print out string x, y times.
This is a comment stating what the function does.
Checkpoint8.2.4.
What are the parameters of the following function?
def print_many(x, y):
"""Print out string x, y times."""
for i in range(y):
print(x)
i
i is a variable used inside of the function, but not a parameter, which is passed in to the function.
x
x is only one of the parameters to this function.
x, y
Yes, the function specifies two parameters: x and y.
x, y, i
the parameters include only those variables whose values that the function expects to receive as input. They are specified in the header of the function.
Checkpoint8.2.5.
Considering the function below, which of the following statements correctly invokes, or calls, this function (i.e., causes it to run)?
def print_many(x, y):
"""Print out string x, y times."""
for i in range(y):
print(x)
z = 3
print_many(x, y)
No, x and y are the names of the formal parameters to this function. When the function is called, it requires actual values to be passed in.
print_many
A function call always requires parentheses after the name of the function.
print_many("Greetings")
This function takes two parameters (arguments)
print_many("Greetings", 10):
A colon is only required in a function definition. It will cause an error with a function call.
print_many("Greetings", z)
Since z has the value 3, we have passed in two correct values for this function. "Greetings" will be printed 3 times.
Checkpoint8.2.6.
True or false: A function can be called several times by placing a function call in the body of a for loop.
True
Yes, you can call a function multiple times by putting the call in a loop.
False
One of the purposes of a function is to allow you to call it more than once. Placing it in a loop allows it to executed multiple times as the body of the loop runs multiple times.