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 are some function is going to add name one plus name 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.
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 lying to hate 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 nine 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
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 tax write a prints 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 additions.
Well 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 summed.
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
will 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 5 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 7.
And it's going to say hey I want a variable total and I want that total variable to equal some 10 5.
And before we even assign anything to total Python interpreter is going to say some.
What's some again.
Oh I remember here it's in memory OK at some.
I'm gonna give it argument's 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 Number 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 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 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 wanted to if I want to make this even simpler I can go some instead of total.
We'll be right here and look at that.
Now I have it all in one line and if I click run again it 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 num 1 and num to an end here we finally return number one and
number two.
Let's test the set.
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 I
get none now.
Why is that.
And this should be obvious to you.
Because here I'm saying hey I want you to define some.
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
func.
So if we give it num 1 and num 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 in the next one by
one last thing a return keyword 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 alright I'm exiting
out of this function with this value just a little heads up.