‫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 num 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?
‫Well, 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 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 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 nine?
‫If we change this to ten 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.
‫Hi.
‫And if I had run here.
‫I return none because I don't have that return statement.
‫However, my function does something a modifies.
‫Well, the look of this text right?
‫It prints hi 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 something.
‫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 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 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 some ten 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 ten 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 OC at some I'm going to give it arguments ten 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 ten, 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 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's some?
‫I know what some is.
‫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, let'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 do 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.
‫Okay.
‫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 one and num 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 ten 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.
‫Okay.
‫But I'm not getting the result that I want.
‫Right.
‫And why is that?
‫Because.
‫Well, 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 1020 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 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 an 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 num 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.
‫Bye bye.
‫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 called, it's going to say, all right,
‫I'm exiting out of this function with this value.
‫Just a little heads up.