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