Check out a free preview of the full Python Fundamentals course
The "Function Arguments" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
Nina demonstrates how to add inputs to functions.
Transcript from the "Function Arguments" Lesson
[00:00:00]
>> Nina Zakharenko: Okay, so we've talked about calling functions with and without arguments. And you also know how to store the returned value of a function, right? You just assign it to a variable just like anything else. Okay, let's talk about Function Arguments.
>> Nina Zakharenko: So let's say I have my add number function and it takes an x and a y and it returns, x + y.
[00:00:38]
If I can add numbers with 3 and 5, I'll get an 8. What if I call add numbers without passing in the second parameter? What kind of error do I get? Y'all have a rupple so y'all have the answers right under your fingertips.
>> Student: It's the argument?
>> Nina Zakharenko: That's right, so it’s a type error, that’s the class, see that right here.
[00:01:10]
It's missing one required positional argument, and helpfully it even tells us which one.
>> Nina Zakharenko: So any arguments defined this way are required. And it doesn't matter what order you pass them in, when you call them they're just gonna be x and y in order within the scope of the function.
[00:01:38]
>> Nina Zakharenko: Python also has key word arguments with default values. Which is really helpful, we can optionally pass in the argument if we want. If we don't want to, there is a default available for us. So let's say we have a function called say_greeting, and it takes a greeting and a name.
[00:02:02]
>> Nina Zakharenko: And we're gonna print out the f string of {greeting}, {name}.
>> Nina Zakharenko: Is everyone getting the hand of these f strings? Yeah, awesome, I thought so.
>> Nina Zakharenko: Okay, so I can call, say, greeting with hello and Nina. It'll print out Hello, Nina.
>> Nina Zakharenko: Let's say I didn't want to pass in Hello all the time.
[00:02:34]
Hello was be used most of the time by default when calling this function. But I wanted to have the option of changing my greeting if I wanted to.
>> Nina Zakharenko: So we're gonna make a new function.
>> Nina Zakharenko: We're going to call it say greeting. If I define those function in the rupple, just know that it will overwrite the other function I've defined here, and that's because they have the same name.
[00:03:05]
So now I'm going to take a name, right, cuz that's kind of the variable of the equation here. And I'm also gonna provide a default argument. Default arguments or arguments with default values always come last.
>> Nina Zakharenko: To pass in a default argument, you just give it a name, equals, and then a string or a number, kind of whatever you want here.
[00:03:38]
And I'm gonna print out the same thing I did before.
>> Nina Zakharenko: Okay, now I can call say greeting with Nina. What will this print out?
>> Student2: Hello, Nina.
>> Nina Zakharenko: That's right, why? Because hello is our default argument. I can also pass in a greeting now. Let's say Bonjour.
[00:04:18]
>> Nina Zakharenko: So unlike the required arguments where I had to have the exact right amount or I got an error, this default argument, it's optional. If I don't provide it, I will get the default value specified here. But if I do, that gets overwritten.
>> Nina Zakharenko: It's really useful, right?
[00:04:41]
I use this a ton in my code.
>> Nina Zakharenko: Just make sure that your default arguments always come last. If you don't declare them in this way, you're going to get a syntax error.
>> Nina Zakharenko: And just quickly show you what that is. Say I had a say_greeting_bad and I tried to say greeting='Hello' and then the required argument of name.
[00:05:19]
Let me give it a.
>> Nina Zakharenko: Let me give the function some content so it doesn't error out early. We'll see here syntax error, non-default argument follows default argument. And now you all know what that means, right, wrong order.
>> Nina Zakharenko: In my say_greeting function here, do I still have to provide this name argument?
[00:05:57]
>> Nina Zakharenko: What do you guys think?
>> Student3: You mean when you call it?
>> Nina Zakharenko: Yes.
>> Student3: Yes.
>> Nina Zakharenko: Okay, I do, right, because positional arguments are always required. If I try to call say_greeting with nothing, then I get my type error, missing one required positional parameter. So introducing default arguments doesn't change anything about that.
[00:06:21]
But you can also pass in none, some, or all of the keyword arguments, right? Keyword arguments are optional. So earlier today we saw a function that created a query for the GitHub API and it took a few QR tasks. Let's say the language is JavaScript. The minimum number of stars is 50 and the sort is descending.
[00:06:59]
>> Nina Zakharenko: Now we can return a f string. Let's just say the language is the language, followed by the number of stars and the sort.
>> Nina Zakharenko: What will I see printed out if I call this with no arguments?
>> Student3: You'll see the default values.
>> Nina Zakharenko: The default values, that's exactly right.
[00:07:34]
It's JavaScript 50 and descending. But I can pass in none. One, let's say I want the language to be Python. What will the sort and the number of stars be? You guys?
>> Student: 50.
>> Nina Zakharenko: Yep, I can also pass in all of these arguments.
>> Nina Zakharenko: And notice that they don't have to be in the same order, that's because they're labeled.
[00:08:09]
Right, so I'm being very explicit about what I'm passing in. You can also, let's say I had a function foo here. It took one positional argument and one default argument.
>> Nina Zakharenko: I can also use the label of the positional argument if I wanted to. So I could call foo with 3.
[00:08:41]
That just automatically sets a to 3 within this function, and b to 5. I could call foo,
>> Nina Zakharenko: By setting 3 in the right position and then passing in b as 6. In Python also if you just wanna be extra explicit about what you're doing, you can pass in the label of A, right?
[00:09:11]
So the positional argument you can pass in that label and you can pass on the label of the keyword argument, it does the same thing.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops