1 00:00:00,240 --> 00:00:00,880 Welcome back. 2 00:00:01,410 --> 00:00:05,700 In this video, I want to talk about an important concept, which is return. 3 00:00:06,210 --> 00:00:11,000 It's a key word in Python that we are going to see a lot when working with functions. 4 00:00:11,280 --> 00:00:12,170 So let's have a look. 5 00:00:12,360 --> 00:00:19,080 If I let's say I want to create a function called sum, which, by the way, already exists in Python, 6 00:00:19,260 --> 00:00:23,880 but we're going to create our own and I'm going to say number one for number one. 7 00:00:23,910 --> 00:00:35,190 And number two, well, our sum function is going to add nine one plus num two, nice and simple function. 8 00:00:36,180 --> 00:00:42,240 Now, if I run some with number four and five, guess what's about to happen? 9 00:00:43,390 --> 00:00:45,100 I'm going to hit run. 10 00:00:46,690 --> 00:00:53,740 And nothing happens, which I mean, might not be surprising to you, because we're not printing anything, 11 00:00:53,740 --> 00:00:53,980 right. 12 00:00:54,130 --> 00:00:56,440 But what if I run print here? 13 00:00:56,800 --> 00:00:58,280 Will this change? 14 00:00:58,420 --> 00:00:58,840 Let's see. 15 00:01:01,830 --> 00:01:04,290 Hmm, I get none. 16 00:01:05,710 --> 00:01:09,980 Is that what you expected and we've seen none before. 17 00:01:10,030 --> 00:01:17,860 Right, I mean, remember when we had a list, let's say one to three, and then we did something like 18 00:01:18,130 --> 00:01:19,360 clear on it? 19 00:01:20,330 --> 00:01:28,760 In our Ed, it gave us this little arrow and said none, and we talked about this idea of on lists, 20 00:01:29,000 --> 00:01:35,540 something like clear changes the actual list and clears everything and empties the list. 21 00:01:35,870 --> 00:01:39,020 But it doesn't return a new list. 22 00:01:39,020 --> 00:01:45,060 It just modifies the existing list, but instead just returns none to us. 23 00:01:45,890 --> 00:01:48,680 Well, functions are just like that. 24 00:01:49,190 --> 00:01:52,430 They always have to return something. 25 00:01:53,000 --> 00:02:01,310 And when they don't return anything like there's no return keyword here, it automatically returns none. 26 00:02:02,260 --> 00:02:09,340 So if we don't have something like return here, it's always going to return nothing. 27 00:02:10,250 --> 00:02:14,720 But if we add return, it's going to say as soon as we get to, like, to hey. 28 00:02:15,550 --> 00:02:23,110 I want you to exit this function, and when you exit this function, I want you to return whatever this 29 00:02:23,110 --> 00:02:25,780 expression gives us, if I click, run. 30 00:02:29,330 --> 00:02:31,280 If we change this to 10. 31 00:02:32,160 --> 00:02:34,140 Plus five, we get 15. 32 00:02:35,020 --> 00:02:41,430 And this is a good general rule with functions, because functions can do two things right? 33 00:02:41,650 --> 00:02:44,550 One, it can return something. 34 00:02:44,620 --> 00:02:51,010 So at the end of the function, whatever, after the function gets run, we return something either 35 00:02:51,010 --> 00:02:58,480 Nunn or some sort of a value, some sort of a data type, or we can have a function that doesn't return 36 00:02:58,480 --> 00:03:01,240 anything but perhaps modifies something. 37 00:03:01,810 --> 00:03:04,600 So, for example, I could print. 38 00:03:07,680 --> 00:03:09,330 And if I had run here. 39 00:03:10,590 --> 00:03:18,640 I return none because I don't have that return statement, however, my function does something it modifies. 40 00:03:18,660 --> 00:03:23,730 Well, the look of this tax, right, a prince high for us. 41 00:03:24,480 --> 00:03:31,680 So a function either modifies something in our program or returns something. 42 00:03:32,900 --> 00:03:38,390 And this is a topic that is quite advanced that we'll get into later on when we talk about functional 43 00:03:38,390 --> 00:03:49,640 programming, but a good rule of thumb with functions is this a function should do one thing really 44 00:03:49,640 --> 00:03:49,930 well. 45 00:03:51,040 --> 00:03:55,750 And usually a function should return something. 46 00:03:57,650 --> 00:04:04,790 Now, this doesn't mean that any function that maybe does multiple things or any function that doesn't 47 00:04:04,790 --> 00:04:12,140 return something is bad, but it's generally good practice to make your code simple and readable, to 48 00:04:12,140 --> 00:04:13,340 have these two rules. 49 00:04:14,030 --> 00:04:23,030 For example, here, if I have print and addition, will, this function is doing multiple things. 50 00:04:23,030 --> 00:04:26,390 It's worrying about printing and it's also worrying about something. 51 00:04:26,840 --> 00:04:31,880 And the definition this function is called some. 52 00:04:32,060 --> 00:04:39,800 I mean, if I didn't know or I didn't see this and all I did was run some, I think having printing 53 00:04:39,800 --> 00:04:44,740 high would be surprising because the name doesn't really describe what it does. 54 00:04:44,750 --> 00:04:46,690 I mean, I'm expecting this to be some time. 55 00:04:46,760 --> 00:04:49,040 Why is my program saying hi? 56 00:04:49,610 --> 00:04:54,560 So we want to make sure that our function does one thing and one thing only really? 57 00:04:54,560 --> 00:04:59,450 Well, because as we'll see later on, we can combine and run different functions together. 58 00:05:00,260 --> 00:05:07,880 And then also we want to make sure that we return something, because this way, what we can do is we 59 00:05:07,880 --> 00:05:12,610 can actually assign this return value to a variable. 60 00:05:13,100 --> 00:05:18,620 For example, I can say total equals to some 10. 61 00:05:21,630 --> 00:05:28,640 And because this function returns something and when it's done, running is going to have 15 as a value, 62 00:05:28,650 --> 00:05:30,960 that's the same thing as what we're doing here. 63 00:05:32,360 --> 00:05:38,210 And here's the cool part now I can run maybe some 10. 64 00:05:39,120 --> 00:05:40,800 Plus total. 65 00:05:44,150 --> 00:05:45,830 Now, let's go through this one by one. 66 00:05:47,820 --> 00:05:54,450 I define my function, the Python interpreter is going to say, all right, I know what some is now 67 00:05:54,480 --> 00:05:55,520 you've defined it. 68 00:05:55,560 --> 00:05:56,880 I'm going to keep it in memory. 69 00:05:57,030 --> 00:05:59,400 It's going to keep going, keep going until line seven. 70 00:05:59,640 --> 00:06:05,820 And it's going to say, hey, I want a variable total and I want that total variable to equal some 10 71 00:06:05,820 --> 00:06:06,200 five. 72 00:06:06,660 --> 00:06:12,300 And before we even assign anything to total python, interpreter is going to say some what? 73 00:06:12,300 --> 00:06:12,870 Some again. 74 00:06:13,110 --> 00:06:14,310 Oh, I remember here. 75 00:06:14,310 --> 00:06:19,320 It's in memory, OK, at some I'm going to give it arguments 10 and five. 76 00:06:19,830 --> 00:06:22,020 So it's going to go run that function. 77 00:06:22,020 --> 00:06:26,490 As soon as we end this closing bracket, it's going to go into the sun. 78 00:06:26,490 --> 00:06:29,520 It's going to say, hey, no one is going to equal ten. 79 00:06:29,850 --> 00:06:32,690 Hey, number two is going to equal five. 80 00:06:33,630 --> 00:06:41,130 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 81 00:06:41,130 --> 00:06:42,080 these variables are. 82 00:06:42,750 --> 00:06:47,580 And then once you're done with that and this turns into 15, I want you to return. 83 00:06:48,450 --> 00:06:54,390 So the Python interpreter, finally, when it's done calling some, is going to say, hey, assign the 84 00:06:54,390 --> 00:06:59,480 value of 15 to the variable total and then it's going to go to the next line. 85 00:06:59,580 --> 00:07:01,140 Hey, I want to print something. 86 00:07:01,140 --> 00:07:02,070 What do you want to print? 87 00:07:02,130 --> 00:07:04,560 Well, I want to print this. 88 00:07:05,100 --> 00:07:07,770 It's going to say, hey, what some I know what's on this. 89 00:07:08,130 --> 00:07:09,030 I have it in memory. 90 00:07:09,300 --> 00:07:11,280 And then again, it's going to say ten. 91 00:07:11,550 --> 00:07:13,620 And then the next argument is going to be total. 92 00:07:13,920 --> 00:07:15,030 Hey, what's total? 93 00:07:15,060 --> 00:07:16,470 Let me go memory and find out. 94 00:07:16,500 --> 00:07:17,760 Oh, it's 15. 95 00:07:18,060 --> 00:07:19,890 So it's like saying 50. 96 00:07:21,210 --> 00:07:31,620 And this is the power of functions, right, I'm able to return things and assign them to variables 97 00:07:31,830 --> 00:07:36,660 and call them like this and I can keep running them if I want to. 98 00:07:36,720 --> 00:07:39,210 If I want to make this even simpler, I can go. 99 00:07:39,450 --> 00:07:42,810 Some instead of total will be right here. 100 00:07:43,470 --> 00:07:45,090 And look at that now. 101 00:07:45,120 --> 00:07:48,300 I have it all in one line and if I click run. 102 00:07:49,310 --> 00:07:50,960 Again, it's still works. 103 00:07:52,250 --> 00:07:55,970 OK, but what if we do something like this? 104 00:07:57,660 --> 00:08:09,330 What if I say define some and then I'll say define inside of here another function and this function 105 00:08:09,600 --> 00:08:12,780 takes number one and number two. 106 00:08:14,190 --> 00:08:19,380 And in here, we finally return number one and number two. 107 00:08:20,720 --> 00:08:22,520 Hmm, let's test this out. 108 00:08:22,550 --> 00:08:23,630 What do you think will happen? 109 00:08:24,350 --> 00:08:30,440 Well, first, let's say total equals some 10 plus 20. 110 00:08:32,260 --> 00:08:34,300 And then I'm going to print total. 111 00:08:35,760 --> 00:08:36,660 If I click, run. 112 00:08:41,170 --> 00:08:47,960 Now, why is that and this should be obvious to you, because here I'm saying, hey, I want you to 113 00:08:47,960 --> 00:08:51,460 define something and what are we going to do with the function? 114 00:08:51,460 --> 00:08:55,510 Some well, inside of here, I'm going to define another function. 115 00:08:55,930 --> 00:08:59,110 But this return statement is actually not run. 116 00:08:59,320 --> 00:09:03,700 Right, because all we're doing inside of this block, this code block. 117 00:09:04,530 --> 00:09:10,320 Is this part we're defining the function, but we're never calling another function. 118 00:09:11,750 --> 00:09:14,030 So this returns none. 119 00:09:15,490 --> 00:09:20,860 And we've lost the ability to well, to call this function. 120 00:09:20,890 --> 00:09:22,390 So how can we solve this? 121 00:09:23,470 --> 00:09:25,870 One way is to return, right? 122 00:09:26,080 --> 00:09:33,130 So at the end here, we have to make sure that the indentation is right and we're going to return another 123 00:09:33,340 --> 00:09:33,730 func. 124 00:09:34,120 --> 00:09:36,670 So let's add return and see what happens here. 125 00:09:37,330 --> 00:09:38,440 If I click, run. 126 00:09:40,650 --> 00:09:48,840 All right, I get this function now, so total is going to equal this function that we returned and 127 00:09:48,840 --> 00:09:49,590 it's in memory. 128 00:09:51,290 --> 00:09:54,720 OK, but I'm not getting the result that I want, right? 129 00:09:54,760 --> 00:09:56,030 And why is that? 130 00:09:56,030 --> 00:10:01,160 Because while we're not calling this function so there's a few ways that we can do this. 131 00:10:01,460 --> 00:10:06,170 We can either give it the parameters or the arguments in here. 132 00:10:06,170 --> 00:10:08,170 So let's say 10, 20. 133 00:10:08,210 --> 00:10:09,200 And if I click, run. 134 00:10:10,460 --> 00:10:19,450 I get 30 or if we want to keep whatever we give it here, I can just simply say another funk. 135 00:10:19,850 --> 00:10:24,860 So if we give it number one and number two and I click run. 136 00:10:26,340 --> 00:10:28,620 Hey, look at that, I get 30. 137 00:10:29,650 --> 00:10:35,740 Now, this video you might have to watch over and over, because it does stretch your mind a little 138 00:10:35,740 --> 00:10:42,010 bit, and to be honest, this function is not very good because it's well, it's very confusing. 139 00:10:42,430 --> 00:10:44,970 Your function should be easy to understand. 140 00:10:45,490 --> 00:10:54,370 So we might want to change this up a bit, maybe instead over here of having the same parameter names 141 00:10:54,370 --> 00:10:55,660 just to clarify it. 142 00:10:55,990 --> 00:10:59,860 Maybe we can say and one and and two. 143 00:11:00,460 --> 00:11:01,570 And then here we do. 144 00:11:01,570 --> 00:11:10,840 And one plus and two to clarify that when we return this other function, I'm using the parameters, 145 00:11:10,840 --> 00:11:13,390 number one, and number two that we called here. 146 00:11:14,610 --> 00:11:19,980 Because this definition of a function doesn't really care what these things are. 147 00:11:20,250 --> 00:11:23,460 It only cares when another function gets called. 148 00:11:24,460 --> 00:11:30,160 Now, if you need to pause the video and look at this and maybe practice this a little bit, that's 149 00:11:30,160 --> 00:11:31,540 completely understandable. 150 00:11:31,840 --> 00:11:34,630 But I wanted to show you the power of return. 151 00:11:34,990 --> 00:11:41,830 You can have nested functions just like you could with conditionals, but you have to be careful that 152 00:11:41,980 --> 00:11:47,350 your function either returns something, otherwise it's going to return none. 153 00:11:47,980 --> 00:11:53,500 And most of the time you're going to want to return something from a function so we can do something 154 00:11:53,500 --> 00:11:58,540 like this where we assign it to a variable so we can use it later on in our programs. 155 00:11:59,110 --> 00:12:00,740 I'll see you in the next one by. 156 00:12:03,770 --> 00:12:13,640 One last thing, a return key word automatically exits the function so that in here, if I added another 157 00:12:13,640 --> 00:12:17,270 piece of code like print, hello? 158 00:12:18,830 --> 00:12:25,970 Or maybe even have return five in here, you see that it's still valid, but if I run this. 159 00:12:27,570 --> 00:12:35,730 I still get 30 because the interpreter never gets to line five and six, because as soon as we return 160 00:12:35,730 --> 00:12:39,950 something from a function, it exits that function. 161 00:12:40,470 --> 00:12:47,550 And the reason that this part doesn't exit the function is because, remember, inside of this function, 162 00:12:47,820 --> 00:12:48,830 we're defining. 163 00:12:48,990 --> 00:12:51,170 So we're not actually running this part. 164 00:12:51,210 --> 00:12:57,300 We're just saying, hey, when we see another function coming up in our code, we know what it is, 165 00:12:57,300 --> 00:12:58,990 but we're not going to run it. 166 00:12:59,010 --> 00:13:00,270 So it's going to look like this. 167 00:13:00,480 --> 00:13:05,040 And as soon as it sees the return statement, when it gets cold, it's going to say, all right, I'm 168 00:13:05,040 --> 00:13:08,310 exiting out of this function with this value. 169 00:13:09,650 --> 00:13:10,880 Just a little heads up.