Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Positional Arguments

For more information about topics covered in this lesson, you can check out these resources:

00:00 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:24 We provided an argument to describe exactly what it is we want printed each time.

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.

00:46 You specify that a function will take positional arguments through a comma-separated parameter list in the function header.

00:57 Then, these parameter variables will be given values when the function is called.

01:05 I’ll be defining this function in my REPL. You can define functions interactively or in files. This is a simple function that I will just create within my interactive environment.

01:19 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.

02:07 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.

02:29 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, qty, item, and 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.

03:06 We’re going to use a print() statement and I’m going to use something called an f-string to format this output, and I’ll describe what I’m doing as I type it.

03:16 But for more information on f-strings, you should check one of the links down below this video. But an f-string allows me to specify how variable values should be displayed inside of a string.

03:30 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.

03:56 And then to complete my sentence, I want to say how much this all will cost.

04:02 Now for 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.

04:16 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:38 So, there’s the brace that closes the location for where I want price. There’s the quote that ends my string literal with the additional f-string formatting features.

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.

05:06 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.

05:23 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 6. For item,

05:40 let’s use the string 'banana', and we’ll say that they cost 1.74.

05:46 So now when this function executes, the variable qty is going to have the value 6, the variable item is going to have the value 'banana', and the variable price is going to have the value 1.74.

06:03 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 bananaI should have typed 'bananas', I’ll do that next time—cost $1.74.

06:23 These are referred to as positional arguments because the order in which you specify the values matters. If I want to say 'bananas', 6, and then 1.74, 'bananas' is going to be used for qty, 6 is going to be used for item, and 1.74 is going to be used for price because that’s the position that those argument values were given in.

06:50 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 'bananas',

07:14 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 positionactually caused the function to crash. Because this function has named three parameters, I have to provide it three argument values.

07:51 So, for example, if I leave off one of the arguments, I will get an error indicating that the function was expecting one more positional argument. If I specify too many,

08:13 I’ll also get an error.

08:16 Four arguments were given, but f() only takes three.

08:24 Next, we’ll talk about another way to provide arguments, and that’s through keyword arguments.

Become a Member to join the conversation.