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 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 called name
and one 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 want 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 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 an f-string 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 you can simply highlight a word, or highlight a sentence, and then use the open curly
brace to actually add the brace around both sides of the word.
And you'll notice that I did the same thing with the quotation mark.
So 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 that 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 let's put Jack Bauer.
And then we can add the second piece of data and separate it with a comma.
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 Nowhere 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's 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 to 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 this,
and "Jack Bauer" is now assigned to location, which is why it printed the 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 argument 1,
2 and 3,
Then it means that our variables that gets created will be a = 1, b = 2, and c = 3
.
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 is 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 = 1,
b = 2, and c = 3.
And now when we actually change the order around, it doesn't matter how we order it, it's still going
to abide by these bindings.
So c will still be 3and a will still be 1.
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 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 judgement 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.