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 or run, you
see that I get Hello, Smiley face, Andre, which maybe is not what we want it 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 emoji.
However, there's something called.
Keyword arguments.
And keyword arguments allow us to, well, not worry about the position, what do I mean, what I can
do something like this, I can say, say hello.
And in here, give it a key word, any value.
So I'm going to say emoji equals to, let's say, the smiley face.
And then Colma 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 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 with 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 emoji will be.
This little angry Darth Vader, well, it doesn't really look like Darth Vader.
But this is the emoji that we are going to 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.
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 in emoji because you were called
without any arguments, well, in that case, make default Darth Vader and may make default emoji.
This little emoji, if I use let's 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 Dipple parameters allow us to keep our functions a little bit more safe because we make
sure that when we use a variable, we're going to have a 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.