Instructor: Welcome back.
In this video, I wanna talk about an important concept,
which is return.
It's a key word in Python
that we are going to see a lot when working with functions.
So let's have a look.
If I, let's say, wanna create a function called sum
which by the way already exists in Python
but we're gonna create our own.
And I'm going to say num one for number one and num two.
Well, our sum function is going to add num one
plus num two, nice and simple function.
Now, if I run sum with number four and five
guess what's about to happen?
I'm going to hit run and nothing happens
which I mean might not be surprising to you
because we're not printing anything, right?
But what if I run print here, will this change?
Let's see.
Hmm, I get none.
Is that what you expected?
And we've seen none before, right?
I mean, remember when we had a list,
let's say one, two, three
and then we did something like clear on it
in our editor it gave us this little arrow and said none.
And we talked about this idea of on lists,
something like clear changes the actual list
and clears everything, and empties the list
but it doesn't return a new list,
it just modifies the existing list.
But instead just returns none to us.
Well, functions are just like that.
They always have to return something.
And when they don't return anything,
like there's no return keyword here,
it automatically returns none.
So if we don't have something like return here
it's always going to return nothing.
But if we add return, it's going to say,
as soon as we get to line two,
Hey, I want you to exit this function,
and when you exit this function,
I want you to return whatever this expression gives us.
If I click run, I get none.
If we change this to 10 plus five, we get 15.
And this is a good general rule with functions
because functions can do two things, right?
One, it can return something.
So at the end of the function,
whatever, after the function gets run,
we return something either none or some sort of a value,
some sort of a data type
or we can have a function that doesn't return anything,
but perhaps modifies something.
So for example, I could print hiii,
and if I hit run here, I return none
because I don't have that return statement.
However, my function does something, it modifies well
the look of this text, right?
It prints hiii for us.
So a function either modifies something in our program
or returns something.
And this is a topic that is quite advanced
that we'll get into later on
when we talk about functional programming.
But a good rule of thumb with functions is this,
a function should do one thing really well
and usually a function should return something.
Now, this doesn't mean that any function
that maybe does multiple things
or any function that doesn't return something is bad
but it's generally good practice to make your code simple
and readable to have these two rules.
For example, here, if I have print and addition,
well, this function is doing multiple things.
It's worrying about printing
and it's also worrying about summing.
And the definition, this function is called sum.
I mean, if I didn't know or I didn't see this
and all I did was run sum, I think having printing hiii
would be surprising because the name doesn't really
describe what it does.
I mean, I'm expecting this to be summed.
Why is my program saying hiii?
So we wanna make sure that our function does one thing
and one thing only really well.
Because as we'll see later on,
we can combine and run different functions together.
And then also we wanna make sure that we return something.
Because this way what we can do
is we can actually assign this return value to a variable.
For example, I can say total equals two, sum ten five.
And because this function returns something,
and when it's done running is going to have 15 as a value.
That's the same thing as what we're doing here.
And here's the cool part.
Now I can run maybe sum 10 plus total.
Now, let's go through this one by one.
I define my function.
The Python interpreter is going to say,
All right I know what sum is now
you've defined it, I'm going to keep it in memory.
It's going to keep going, keep going, until line seven.
And it's going to say, Hey, I want a variable total
and I want that total variable to equal sum ten five.
And before we even assign anything to total,
Python interpreter is going to say,
Sum.
What's sum again?
Oh, I remember here.
It's in memory.
Okay, at sum, I'm gonna give it arguments 10 and five.
So it's going to go run that function
as soon as we end this closing bracket.
It's going to go into the sum,
It's going to say, Hey, num one is going to equal 10.
Hey, num two is going to equal five.
Now we go to line two and it's going to say,
Hey I want you to add 10 plus five
because I know what these variables are,
and then once you're done with that and this turns into 15,
I want you to return.
So the Python interpreter, finally, when it's done
calling Sum is going to say,
Hey assign the value of 15 to the variable total.
And then it's going to go to the next line.
Hey, I wanna print something.
What do you wanna print?
Well, I wanna print this.
It's going to say, Hey, what's sum?
I know what sum is, I have it in memory.
And then again, it's going to say 10.
And then the next argument is going to be total.
Hey, what's total?
Let me go in memory and find out, Oh, it's 15.
So it's like saying 15.
And this is the power of functions, right?
I'm able to return things and assign them to variables
and call them like this.
And I can keep running them if I want to.
If I wanna make this even simpler,
I can go sum instead of total, will be right here.
And look at that.
Now I have it all in one line, and if I click run,
again, it still works.
Okay, but what if we do something like this?
What if I say define sum, and then I'll say,
define inside of here another function.
And this function takes num one and num two.
And in here we finally return num one and num two.
Hmm, Let's test this out.
What do you think will happen?
Well, first, let's say total equals sum 10 plus 20
and then I'm going to print total.
If I click run, I'll get none.
Now why is that?
And this should be obvious to you because here I'm saying
Hey, I want you to define sum.
and what are we going to do with the function sum?
Well inside of here, I'm going to define another function
but this return statement is actually not run, right?
Because all we're doing inside of this block
this code block, is this part.
We're defining the function
but we're never calling another function.
So this returns none.
And we've lost the ability to, well, to call this function.
So how can we solve this?
One way is to return, right?
So at the end here,
we have to make sure that the indentation is right
and we're going to return another func.
So let's add return and see what happens here.
If I click run, all right, I get this function now.
So total is going to equal this function that we returned
and it's in memory.
Okay, but I'm not getting the result that I want, right?
And why is that?
Because while we're not calling this function
So there's a few ways that we can do this.
We can either give it the parameters
or the arguments in here.
So let's say 10, 20, and if I click run,
I get 30.
Or if we wanna keep whatever we give it here,
I can just simply say another func.
So if we give it num one and numb two, and I click run,
hey look at that, I get 30.
Now this video, you might have to watch over and over
because it does stretch your mind a little bit.
And to be honest, this function is not very good
because it's, well, it's very confusing.
Your function should be easy to understand.
So we might want to change this up a bit.
Maybe instead over here of having the same parameter names.
Just to clarify it, maybe we can say N one and N two.
And then here we do N one plus N two
to clarify that when we return this other function
I'm using the parameters num one and NUM two
that we called here.
Because this definition of a function
doesn't really care what these things are.
It only cares when another function gets called.
Now, if you need to pause the video and look at this
and maybe practice this a little bit,
that's completely understandable,
but I wanted to show you the power of return.
You can have nested functions
just like you could with conditionals,
but you have to be careful
that your function either return something,
otherwise it's going to return none.
And most of the time you're going to want
to return something from a function.
So we do something like this
where we assign it to a variable
so we can use it later on in our programs.
I'll see you in the next one.
Bye bye.
One last thing.
A return keyword automatically exits the function.
So that in here,
if I added in another piece of code, like print hello
or maybe even have return five in here,
you see that it's still valid, but if I run this,
I still get 30
because the interpreter never gets to line five and six.
Because as soon as we return something from a function
it exits that function.
And the reason that this part doesn't exit the function
is because remember, inside of this function we're defining.
So we're not actually running this part.
We're just saying,
Hey when we see another function coming up in our code
we know what it is, but we're not gonna run it.
So it's gonna look like this.
And as soon as it sees the return statement
when it gets called, it's going to say,
All right I'm exiting out of this function with this value.
Just a little heads up.