- Read Tutorial
- Watch Guide Video
- Complete the Exercise
What this means is that the Kristine string right here
is going to be passed to this first argument and then it's going to be used throughout the entire function because we passed it in as the first element. And so Hudgens is going to be passed into the last value and please remember there is nothing special about the words first and last I could say F and L if I wanted to and then use F an L in the print statement. I'm not going to because I don't think that that's very explicit but just so you know you don't have to pass in first or second and those kinds of names. These are completely determined by what you think explains the value the best in the function.
But everything that we've done has relied on that position and that is fine for very small functions. However, when it comes to working with larger programs and more advanced functionality having positional arguments can lead to some confusion imagine that you are building out a function and it didn't have simply two arguments it had another one and a fourth one and you get the idea just keeps on going on and on.
If you were to call a function like that and pass in values like we're doing with full_name('Kristine', 'Hudgens') where we're calling them in and the position determines the mapping then it can lead to a number of bugs because you may think that you're calling and passing in values in the right order but you might skip one and then all of a sudden your entire program is thrown off. And so that is something that you definitely want to avoid. And in python named arguments give you the ability to be much more explicit with this mapping.
And so what I can do here is instead of simply passing a string in I can pass in whatever the name is. And so, in this case, if I want this to go and be mapped to the first name then I can have the first name variable just like this. And then if I want to do it the same way here for last then what I'm going to do is the program is going to be able to look inside of this function call and it's going to see that we have set up named arguments and is going to first look for a function argument named first and then it's going to pipe this value into it and then it's going to come to last and it's going to do the same thing.
So if we run this code you can see that it works perfectly
just like before and so this is helpful because now we're being much more explicit with how we're passing our values in.
Now we can even improve this more because if we have named arguments as we have right here we no longer care about the position these are no longer positional arguments. So what that means is I could call the last name variable to pass it in as the first argument. And this is all still going to work perfectly. So if I run this again you can see that the order has not changed the behavior because named arguments explicitly declared the mapping then they allow you to pass in whatever your values are in whatever order you prefer.
One thing I love about the python syntax compared with a few other languages such as Ruby or javascript is with python. You can use name arguments in an optional manner which means that usually in a language such as Ruby if you want to work with named arguments you have to come up to the function definition and then perform some code changes so you need to do something like this where you place a colon at the end of each name and what that's going to do is tell the ruby interpreter that you want these arguments to be named arguments and that means that anyone that calls this function needs to use named arguments.
def full_name(first:, last): print(f'{first} {last}')
But as you can see Python is much more flexible in that manner where all functions can use either named or pure positional arguments and it's completely up to you. My rule of thumb is that if there are more than 2 arguments I will use named arguments simply because it prevents any issues with me placing the values in the wrong order or calling the wrong name or anything related to that. And so I think that is a good rule of thumb and I highly recommend for you to explore it on your own side and see how you can use named arguments in your own functions.
Code
def full_name(first, last): print(f'{first} {last}') full_name('Kristine', 'Hudgens') full_name(first = 'Kristine', last = 'Hudgens') full_name(last = 'Hudgens', first = 'Kristine')