For more information about topics covered in this lesson, you can check out these resources:
Next, we’ll take a look at a way to give functions a little bit more flexibility, and that’s through argument passing. Argument passing allows you to provide data for the function to use. That way, the function won’t always run the exact same way each time. The function’s behavior can vary from one invocation to the next. You wouldn’t want the
print() function, for example, to always print out the same thing.
00:31 The most straightforward way to provide argument values to a function is through positional arguments. These are also called required arguments, and we’ll see other types of arguments in some upcoming lessons.
So, you begin the function with the word
def and then a function name. Now, throughout this course, I’m going to use the very boring function name
f for just about every function that we take a look at. When you’re writing a meaningful function, you want to give it a meaningful name that describes what it does, but here we’re looking at the structure of functions and so we’re just going to use an arbitrary letter for each of the new functions that we create. The author of the companion tutorial that this relates to also does the same thing. Now, the inner C programmer in me wants to call all of these arbitrary functions
foo, but the author of the article uses the word
foo for something else and I’ll be following that as well in this course.
So we’ll just use the letter
f to stand for a function, but keep in mind that a meaningful function should have a meaningful name. But here I want to specify that this function needs argument values to work. In this particular case, I want to specify three, and so we’re going to specify that through parameter names.
We’re going to call the first one
qty to represent some quantity of something, the second one we’re going to call
item, and the third one we’re going to call
price. When I use this function, I’m going to have to give it three argument values: one to represent
qty, one to represent
item, and one to represent
price. In the body of my function,
price become variable names that I can use however I would use other variable names once I know they have a value. In this particular case, I just want to print out the statement that indicates how much something costs.
The first thing I want is at the very beginning of this output, I want the quantity displayed, so I put that in braces and when this function runs, whatever value has been given for
qty is going to be displayed at the very beginning of this string. Following a space, I want to display the value of
item, and so I put the variable name
item in braces.
price, I’m going to do something a little bit different. We’re used to prices having exactly two decimal digits, and there’s no way to guarantee that that’s going to be provided for us when argument values are given in the function call.
The f-string allows me to specify that, using a formatting code of
.2f. What that means is I want a fixed number of decimal digits, specifically two. So no matter what value is given for
price, it’s going to be rounded to two decimal places.
04:50 And there is the closing parenthesis. Notice how this line was already indented for me. Your interactive environment will probably do that, as well as most editors will indent four spaces for the body of your function.
Since this is all that I want to happen, on the next line, I’m just going to hit Enter on a blank line, and that defines my function. So, now I have a function
f() that I can use, and it’s going to display information about how much so many of a particular item cost.
So, let’s do a function call! To call a function, you specify the name and then in parentheses, you’re going to specify values that you would like each of the parameters to have. For example,
qty, in this case, let’s just say
And those values will be used in the body of the function, which in this case is to simply display how many and what items we have and how much they cost. And so we get that
6 banana—I should have typed
'bananas', I’ll do that next time—
These are referred to as positional arguments because the order in which you specify the values matters. If I want to say
6, and then
'bananas' is going to be used for
6 is going to be used for
1.74 is going to be used for
price because that’s the position that those argument values were given in.
And so we get
bananas 6 cost $1.74. I’m not sure that makes sense. Depending on how your function is used, putting parameter values in the wrong or unexpected order could actually cause an error. For example, if I say
f() and then say
6, and then
1.74, and then
I’m actually going to get an error message this time because the body of the function was expecting
price to be something that could be rounded to two decimal places, and a string literal cannot be rounded to two decimal places.
07:30 And so in this particular case, putting the argument values in the wrong order—in the wrong position—actually caused the function to crash. Because this function has named three parameters, I have to provide it three argument values.
Become a Member to join the conversation.