In the last lesson,
we saw a very simple form of a function that allows for an input.
Now in this lesson,
I want to take it even further and I want to create a function that allows for
multiple inputs.
So let's comment out of the previous line of code and let's create a new comment,
functions with more than one input.
And I'm going to create a new function called greet_with.
And in this case, it's going to take two parameters.
It's going to be name and location. If you remember,
this is how we added a parameter previously. Now, as a challenge,
I want you to quickly think about how you might add two parameters,
one's called name and one's called location, into this function
declaration. Pause the video, have a brief think,
and then we'll go through it together. All right.
So we know that if we wanted to add one parameter name,
then this is how we would do it. We would just add it inside the parentheses.
Now, if we wanted to have more than one parameter,
all we have to do is just add a comma and then add the second parameter which we
said was going to be called location.
So now this particular function is going to take two inputs,
the name and the location. And then inside this function,
we're going to use the name to print something like hello name,
and then we're going to print and ask them,
what is it like in their particular location.
See if you can modify this to use the actual parameters and to replace them with
print statements so that we use these parameters inside of our function.
Pause the video now and give that a go. All right.
So essentially we want to create a print statement here and print statements
print strings so we have to add some quotation marks around that text.
And then finally,
I'm going to use a fstring to replace this parameter name inside this string so
that the data that gets passed in gets replaced here, and it says,
hello, whatever their name is.
And then I'm just going to do the same thing over here
and notice that with replit and a lot of other text editors,
you can simply highlight a word or highlight or a sentence,
and then use the open curly brace to actually add brace around both sides of the
word. And you'll notice that I did the same thing with the quotation marks.
So it highlight the whole sentence and then hit the double-quote key
and it will keep adding quotes around both sides of your highlight.
But I only actually need one.
So that is our function completed.
And now it means I can call this function by calling greet_with,
and it's going to prompt me to add both of these inputs.
And the first piece of data is going to be the name
and you can see it's giving you a hint by underlining the name of the parameter
here. And let's put Jack Bauer
and then we can add the second piece of data and separate it with a comma.
And now you can see this underline has moved to the location parameter.
So again, a hint as to what kind of data it wants.
So let's say that Jack Bauer is nowhere. Now,
if we go ahead and run this code,
then you'll see that it's going to print hello and replace name with Jack Bauer.
And then what is it like in nowhere? So no
where gets replaced with this location.
So that means that you can now put in as many pieces of inputs as you want and
modify the functionality of your function to make your function do different
things each time. Now here's a question.
What happens if I call the same function greet_with,
but I switch the order of the data that I give it.
So let's say the first piece of data I give it is nowhere,
and then the second piece of data is Jack Bauer.
So we've just switched the order of these pieces of data. Now,
what do you expect to happen?
Pause for a moment and have a think about what you expect to be printed in here
and then continue. All right,
so let's click run and you can see that it's now complete nonsense.
Hello, nowhere. What is it like in Jack Bauer? And what actually happened here
is it takes the position of the data, looks at both of these arguments,
and the first argument gets assigned to the first parameter
the second argument gets assigned at the second parameter. So in this case,
when it's actually gone in here, name is now equal to nowhere,
which is why this line printed thi.sAand Jack Bauer is now assigned to location
which is why it printed this second line like so. And in Python programming,
this is called a positional argument because when we call the function,
we haven't specified anywhere which particular parameter we want to associate
these pieces of data with. So it's just gone and looked at the position.
Now this is the default way of calling functions, because on one hand,
when you're typing out the code,
you get the hints here as to which piece of data you need to enter.
But also you can refer to the function and look at the order of the parameters.
Even if we had more inputs, let's say in this case, we had a,
b and c, and we put the arguments one, two and three,
then it means that our variables that gets created
will be a equals one,
b equals two and c equals three.
Now, if we switch around the order of the arguments in the function call,
now what will happen is a is going to be equal to the first argument.
So a is now equal to three, b as equal to the second argument and c is equal to
the third argument. So it might be doing slightly unpredictable things in here.
So whenever you're creating code and you're using these positional arguments,
and you're just inserting the data one by one like this,
and it does something completely unexpected,
then be sure to check your positioning and to make sure that it matches with the
position of the parameters. Now,
what if you wanted to be more clear when you actually call the function
so you don't ever encounter this problem? Well,
you could use something called keyword arguments instead. So now,
instead of just adding the arguments into the function call like this,
we can actually add each of the parameter names and an equal sign to say that
the first parameter a
should be equal to one, b should be equal to two and c equals to three.
And now when we actually change the order around,
it doesn't matter how we order it,
it still going to abide by these bindings. So c
will still be three and a will still be one.
As a challenge, I want you to take this previous function,
greet_with_name and location,
and I want you to call this function down here, but this time,
instead of using positional arguments, I want you to use keyword arguments.
So pause the video and try and give that a go.
All right. So when we call the function, we still use the name.
So it's greet_with, so everything up to the first parentheses is the name.
And then we add in each of these parameter names,
and then we add an equal sign and finally, we give it the actual value.
So let's say Angela,
and then the location is going to be equal to London.
Now, when I hit run,
you can see it does pretty much the same as before. It puts Angela into the name,
London into location. But this time if I switch the order around it
no longer matters. And when I hit run again,
you can see it does exactly the same thing,
because it now knows which argument is associated with which parameter.
So this can make your code less error prone
but it does make each line of code longer.
I recommend using your judgment to figure out when you want to use which type of
argument and depending on the need, you can pick between these two.
Now in the next lesson, I've got a coding exercise for you to put into practice
everything that you've learned so far.
Head over there when you're ready and let's give it a go.