Welcome back.
In this video, I want to 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 I want to create a function called sum, which, by the way, already exists in Python,
but we're going to create our own and I'm going to say number one for number one.
And number two, well, our sum function is going to add nine one plus num two, nice and simple function.
Now, if I run some 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 to three, and then we did something like
clear on it?
In our Ed, 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, like, to 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.
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
Nunn 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.
And if I had 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 tax, right, a prince high 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, will, this function is doing multiple things.
It's worrying about printing and it's also worrying about something.
And the definition this function is called some.
I mean, if I didn't know or I didn't see this and all I did was run some, I think having printing
high would be surprising because the name doesn't really describe what it does.
I mean, I'm expecting this to be some time.
Why is my program saying hi?
So we want to 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 want to 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 to some 10.
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 some 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 some 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 some 10
five.
And before we even assign anything to total python, interpreter is going to say some what?
Some again.
Oh, I remember here.
It's in memory, OK, at some I'm going to 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 sun.
It's going to say, hey, no one is going to equal ten.
Hey, number two is going to equal five.
Now we go to line two and it's going to say, hey, I want you to add ten 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 some, 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 want to print something.
What do you want to print?
Well, I want to print this.
It's going to say, hey, what some I know what's on this.
I have it in memory.
And then again, it's going to say ten.
And then the next argument is going to be total.
Hey, what's total?
Let me go memory and find out.
Oh, it's 15.
So it's like saying 50.
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 want to make this even simpler, I can go.
Some 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's still works.
OK, but what if we do something like this?
What if I say define some and then I'll say define inside of here another function and this function
takes number one and number two.
And in here, we finally return number one and number two.
Hmm, let's test this out.
What do you think will happen?
Well, first, let's say total equals some 10 plus 20.
And then I'm going to print total.
If I click, run.
Now, why is that and this should be obvious to you, because here I'm saying, hey, I want you to
define something and what are we going to do with the function?
Some 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.
OK, 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 want to keep whatever we give it here, I can just simply say another funk.
So if we give it number one and number 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 and one and and two.
And then here we do.
And one plus and two to clarify that when we return this other function, I'm using the parameters,
number one, and number 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 returns 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 can 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 by.
One last thing, a return key word automatically exits the function so that in here, if I added 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 going to run it.
So it's going to look like this.
And as soon as it sees the return statement, when it gets cold, it's going to say, all right, I'm
exiting out of this function with this value.
Just a little heads up.