-: Hi guys and welcome back.
In this video we're going to learn about
return values and functions.
And that is something really confusing
for new Python developers.
Here we've got a function called add
that prints X and Y.
If we call add with the values five and eight
it should print 13.
So let's run that and you'll see
that 13 gets printed out.
However, something that we have been doing
with some other functions in our code
has been assigning the value of it to a variable.
For example result, equal, add, five and eight.
If we then print result you could potentially
expect 13 to be printed out.
However, here's what's gonna happen.
We're going to run line five which is going to
start running the function and it's going to print 13.
Then we're going to run line six
which is again going to run the function,
because here, notice there is a function call
and that is going to run this line,
which is going to print 13.
Then we're going to assign something to the result variable,
and finally we're going to print the result variable.
So we should see 13, 13, and then the value
of the result variable printed out.
So we're going to print out three lines.
And you can see that we get 13, 13, and then None.
None is a special value in Python that means
no value, or missing value, or undeclared value.
Take your pick.
So that means that this function here,
when it finished running did something
that made our result variable be equal to None,
because when we printed result it printed None,
therefore result must be None.
So that is where return values come in.
Functions return None.
That's what they do by default.
That means that whenever you call a function
you can assign it's return value to a variable
or you can use it in another part of your programme,
such as a mathematical expression or something like that,
and that return value is what you get
when you call a function.
So, functions return None by default
which means that the result variable
is being assigned to None.
If we want to do it differently
you're going to have to return your own value.
So we're gonna say return X plus Y
without the brackets instead of print.
Now there's an important thing,
which is that if we remove these lines-
I'm just going to put the hash symbol
in front of them which signals that they are a comment
and they won't run when the code runs.
If we press play now you'll see that
nothing gets printed out, because of course
we've executed the add function
but this function does not print anything.
It just returns something.
So, because we're not printing anything
nothing gets printed out.
Now if we do this, we call the add function
which returns the addition of the two arguments,
five and eight, and then make it equal to
the result variable.
That means that result is going to be equal to
the return value of the function which is gonna be 13.
And then we're going to print 13.
so when we run this code you'll see 13.
At this point I'll say that very often
I will see students doing this,
print X and Y, and then they will try to do this.
Print, add, five and eight.
This is a very common thing I see in my course,
and notice that when you run this code,
you're going to get 13 and then None.
Again, that's because the add function
gets called first so that the print function
knows what to print.
So here it prints 13 and then returns None,
because all functions return None by default.
So you get 13 printed out and then you get None printed out.
And this is very common, so if you're seeing this problem
or you've encountered this before,
now you know why that is.
Let's go back to printing the result
of calling the function and we're going
to make it both print and return.
So now when we do this you're going to
see 13 printed out twice.
One, because the function itself prints 13 out,
and the second one because we're actually
printing the return value of the function
that we've assigned to our variable.
Return not only gives a value back to
the caller at the end of the function,
it actually terminates the function entirely.
So if we were to move this return value
to before the print and then press play
you'll see that only one 13 gets printed out,
because as soon as the function reaches
this return statement it returns and exits the function
and gives the value back to the caller.
So this print function never gets called.
Only this one does.
That is another common problem that
some people will put a return value
elsewhere in the function as well as
the one at the end of the function.
This is totally fine, you can do that.
But just make sure that it is what you want.
Because when you return without a value
you're actually going to return None by default.
So by putting a return here in line two
it means that none of these lines are going to run.
Because the function will terminate when you
encounter a return and this here will become None
because there is a return with no value
which means return None.
So our result will be None and
you'll end up printing None out.
If we press play you'll see that none comes out.
Let's go back to our divide function from the last video,
but now instead of printing out, we're going to return.
So here we've got our divide function
with a dividend and a divisor.
If the divisor is not zero,
we're going to return the division,
otherwise we're going to return the string, you fool.
So if we divide 15 by three and we print the result out
we should see a number coming out
and we do get 5.0.
But if you decide to go for 15 and zero
and then you press play,
you'll see that you fool gets printed out.
This is fine, you can totally do two returns
in a single function as long as only one of them runs,
which is what happens in this if statement.
You can still access both returns depending
on these conditions here.
So again a return will terminate the function
when the line is ran and give the value
returned back to the caller.
So in this case, this function call here
will be replaced, essentially,
by whatever the function returns when it runs.
As an aside it's usually not recommended
to have functions returning multiple
different types of data.
For example this function can return a number or a string.
Just because if you wanted to perform mathematics
on the return value of this function
you will encounter problems if you do return a string.
For example if you wanted to print out the result
of dividing this multiplied by five,
then of course you're going to get
you fool, you fool, you fool, you fool.
However if you change this number
then you'll actually get a valid mathematical number.
So, usually not recommended to return
different types of data from a function.
Although as I said, you can do it,
it's just not advised.
That's everything for this video.
Thank you for joining me in this one,
and I'll see you in the next one.