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.
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.
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.
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.
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.
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.