In the previous videos I showed you how to define a function with parameters.
Now the way I've shown you this and these parameters is what we call positional parameters or we might
want to call them positional arguments and they're called positional arguments because the position
matters right.
If I change our arguments to let's say Andre as the second argument and if I click Run you see that
I get Hello smiley face andre which maybe is not what we wanted to do positional arguments are arguments
that require to be in the proper position.
So if we defined name here first and then emoji Second you have to make sure that the first argument
will be the name and the second argument will be the emotion.
However there is something called key word arguments and keyword arguments allow us to.
Well now worry about the position.
What do I mean.
Well I can do something like this.
I can say say hello and in here give it a keyword and a value.
So I'm going to say emoji equals to let's say the smiley face and then comma name equals to Bebe.
Now if I run this you'll see that I get.
Hello Bebe because I was able to use keyword arguments.
I tell it explicitly hey I want the emoji to be this and name to be this so positional arguments these
matter but keyword arguments.
It doesn't.
Now I argue that this is bad practice because you're making the code more complicated than it needs
to be.
If the definition of the function is to give name an emoji you should follow that practice and not confuse
other developers then just stick to that name and emoji so you can still use keyword arguments but make
sure that they are in order because why wouldn't you just follow what the function tells you.
It's the same result but at least this way you are following Well the standard flow and by the way these
keyword arguments can sometimes be confused we what we call default parameters.
I know it can get really really confusing but this is something that will come naturally to you after
you practice a little bit.
So I'm going to just comment this out and show you how default parameters work default parameters.
Allow us to give right in here as we're defining the function what we want as default in this.
In our case let's say Darth Vader is the default name and the IMO g will be this little angry Darth
Vader.
While it doesn't really look like Darth Vader but this is the emoji that we gonna use.
So what does this do.
Well if I run my program everything works the same.
If I do keyword arguments and run my program nothing changes.
Let's make this a little bit bigger.
However default parameters allow us to do something interesting.
For example if I run a another function say hello but I forget to give it parameters or arguments in
our case.
Well if I run this I get.
Hello.
Darth Vader because with default parameters it says if you're not able to get name and emoji because
you are called without any arguments.
Well in that case make default Darth Vader and make default emoji.
This little emoji if I use lets say just one argument and I click run then well it will fill that need
variable but anything that we don't give it like emoji will be pre defined.
So this is a great way to make sure that your function runs even if it's called the wrong way.
So keyword arguments increases the readability of your code because you know exactly how we're calling
say hello and DFL parameters allow us to keep our functions a little bit more safe because we make sure
that when we use a variable we're gonna have no matter what regardless of how it is being called but
positional arguments are also useful because it's nice and clean right.
We're just following what the function tells us to do.
And it's easy to read.
So there's pros and cons of using each but I hope this demonstrates to you some of the ways that you
can define functions and also call functions.
I'll see in the next one.
Bob.