-: Hi guys, and welcome back.
In this video, we're going to talk about function,
arguments and parameters.
That is, how we can give our functions data
so that they can use it.
For example, the print function makes use of this
so that it can accept a string to print out
into the console.
We're going to learn how to do that in our own functions.
Let me define a function that has two parameters.
What I've done here is I have created a function
called add and inside the brackets I've put the names
of its two parameters.
So that's x and y.
Notice that I have put the word pass inside the function
body, and what that means in Python is do nothing.
It is needed, at least something is needed
inside a function because Python expects
the indented block there.
So that's why I've put that there, but it doesn't
mean anything.
We've got our two parameters here which are x and y.
We have two parameters because they are two variable
names separated by a comma and what happens is that when
Python runs the function, when you call the function itself,
these two variables are created and the function
can use them inside the function body.
When you get to the end of the function body,
the variables disappear.
So what are the values of x and y?
Well, that depends on what you give the function
when you call it.
So for example, if you add and put five comma three,
then these here are all called arguments.
These here are called parameters.
So each argument provides a value to one parameter.
Five is the value of x, and three will be the value of y.
Of course our add function doesn't do anything just now
so we are going to make it do something.
We're going to say result is x plus y,
and then we're going to print result.
So now we have it here our function with two parameters,
and then our function call with two arguments.
When the function call happens, these values get placed
into these variables and the function runs.
So here result will be eight
and then we will print that out.
So let me just run that for you, and you can see that eight
comes out down there.
Let's do another example.
Now a function without any parameters.
So if we have a function such as this say hello function
that doesn't have any parameters,
notice that the brackets are empty.
You cannot give it arguments.
So if you type say hello and you pass in Bob,
this will fail in Python because you are passing an argument
to a function that has no parameters.
So if we run this, you'll see that it says
say hello takes zero positional arguments,
but one was given.
We haven't looked at what positional arguments mean yet,
but Bob is a positional argument.
More on that later on.
So we've given this function one positional argument,
but this function doesn't take any.
It doesn't have any parameters.
So that's why the error happens.
Similarly, if we instead decide to pass in a name,
and we do something like this,
now you can call the function with an argument.
So if we run this, you'll see that we get "Hello, Bob"
because this function prints the string "Hello"
with the name variable inside it, but having a parameter
means that you must pass in an argument.
So you cannot call the function without an argument.
If you do, it says that say hello is missing one required
positional argument and then it gives you the name
of the positional argument.
So it's saying that you need to pass in a value
for this name parameter, but you didn't.
So you should.
So when you put Bob in here, we've said that this is a
positional argument, and the reason why it's called
a positional argument is because if you have multiple
arguments such as these two here, and then you call it
like that, the only way Python has of determining
which parameter takes which value is by position.
So Bob is the first argument or in the first position.
So the name parameter will take the value of Bob,
and Smith is the second argument.
So the second parameter will take the value of that.
So these are called positional arguments
because the position they're in will affect
the value of its relevant parameter.
However, Python also has arguments that are not positional.
They are called keyword or named arguments.
For example, you can do surname equal Bob
and name equal Smith.
Now Python will be able to tell that you want
the surname parameter to take the value of Bob
and the named parameter to take the value of Smith.
These here are named or keyword arguments.
Named or keyword arguments are really helpful
because they allow us to very easily determine
when we are reading this line here which argument
is for which parameter, and indeed if we hadn't
come across this function before, it would also tell us
what the function does to some extent.
If you read this line here, you can see that okay,
you're going to say hello and you're taking a name
and a surname, so you're probably going to print that out.
That allows you to make some inferences about what this
code is going to do.
So this can be really helpful and it's very widely
used in Python.
For example, let's say that you've got a divide function
where if the divisor is not zero, then you're going to print
the dividend by the divisor.
Otherwise, you're going to print something
like "You fool," you can't divide something by zero.
So we've got a function here.
Then you can call this function divide with something
like 15 and zero.
So this may be okay.
You can press play and you get back "you fool."
However, it can be clearer if you tell the reader
of this line which value is which.
Now with something like division it may be obvious
that 15 comma zero is going to divide 15 by zero.
That seems reasonable to a reader I think,
but, again, it can be cleared if you say dividend is 15,
and divisor is zero.
Notice that usually in Python we do not put spaces
around this equal sign here when it's being used
to separate a value from its keyword argument.
As well as passing dividend and divisor like this,
you could reverse them; put the divisor here
and the dividend there and that would be fine,
although maybe a little more bit confusing,
but also not all arguments have to be keyword arguments.
You can have some positional arguments
and then some keyword arguments, but that's exactly it.
The positional arguments have to go first
and the keyword arguments have to go later.
If you do dividend equal 15 and then you skip the keyword
argument there, you're going to get an error and Python
is going to tell you that a positional argument follows
a keyword argument and that is illegal in Python.
So the positional arguments have to go first,
keyword arguments go later.
I recommend that you use keyword arguments
whenever possible just because you're going to make
your code a little bit easier to read,
unless it's really obvious what's going on.
So for example, in a divide function, maybe you don't need
the keyword arguments.
Similarly, in a say hello function that just takes
a person's name, you probably don't need keyword arguments
either, but as soon as you start doing more complicated
things or functions that are maybe not as obvious,
using keyword arguments is really going to help.
So I recommend that you use them.
That's it for this video though.
Thank you for joining me in this one,
and I'll see you next time.