1 00:00:08,090 --> 00:00:15,405 Welcome back. One metaphor I find helpful is to think of a function as a machine. 2 00:00:15,405 --> 00:00:19,515 It may take some inputs, the parameters, 3 00:00:19,515 --> 00:00:21,585 and then when you run it, 4 00:00:21,585 --> 00:00:26,265 that's the shaking that you see in this animation, 5 00:00:26,265 --> 00:00:28,570 it can produce an output. 6 00:00:28,610 --> 00:00:34,300 So, let's look at how to make a function return a value. 7 00:00:34,370 --> 00:00:38,415 So, here's a definition of the square function. 8 00:00:38,415 --> 00:00:44,190 Notice line three, we have 9 00:00:44,190 --> 00:00:51,435 a special word 'return' and then afterwards, any Python expression. 10 00:00:51,435 --> 00:00:55,475 In this case, just a reference to the variable y. 11 00:00:55,475 --> 00:01:00,040 The value of that variable is some Python object, 12 00:01:00,040 --> 00:01:04,085 and that's the value that will be returned from the function. 13 00:01:04,085 --> 00:01:07,689 Let's take a look at line six, 14 00:01:08,090 --> 00:01:12,554 we're invoking the function square, 15 00:01:12,554 --> 00:01:17,120 we're passing in an input, an argument, 16 00:01:17,120 --> 00:01:22,960 that's going to be bound to the first formal parameter name x, 17 00:01:22,960 --> 00:01:26,020 and then we'll execute this function, 18 00:01:26,020 --> 00:01:29,730 we're going to multiply whatever x is, 19 00:01:29,730 --> 00:01:32,710 x is going to be whatever toSquare was, 20 00:01:32,710 --> 00:01:37,805 and we'll multiply it by itself on line two and assign it to the variable y, 21 00:01:37,805 --> 00:01:41,785 we're going to return a value and whatever is returned, 22 00:01:41,785 --> 00:01:44,885 becomes the value of the entire expression, 23 00:01:44,885 --> 00:01:50,370 so that is what is going to be bound to the variable result. 24 00:01:51,790 --> 00:01:56,510 Note that the word result here is not any special word, 25 00:01:56,510 --> 00:01:57,965 that's just a variable name. 26 00:01:57,965 --> 00:02:01,025 Return is a special word, 27 00:02:01,025 --> 00:02:04,850 that means to return a value from the function. 28 00:02:04,850 --> 00:02:08,340 So, let's see that in code lens. 29 00:02:09,200 --> 00:02:13,425 So, we first define the square function, 30 00:02:13,425 --> 00:02:19,630 then we get toSquare is bound to the value 10, 31 00:02:19,910 --> 00:02:22,590 that happened on line five, 32 00:02:22,590 --> 00:02:24,005 and then on line six, 33 00:02:24,005 --> 00:02:27,385 we are going to invoke the square function. 34 00:02:27,385 --> 00:02:29,395 So behind the scenes, 35 00:02:29,395 --> 00:02:32,920 we're going to get an assignment that 36 00:02:32,920 --> 00:02:37,795 the variable x is going to get whatever value toSquare had, 37 00:02:37,795 --> 00:02:40,750 so toSquare was 10, 38 00:02:40,750 --> 00:02:44,720 and so x is going to have the value 10. 39 00:02:47,750 --> 00:02:53,060 We then execute the code that's inside the square function, 40 00:02:53,120 --> 00:02:58,170 we get y is 10 times 10, or a 100, 41 00:02:58,170 --> 00:03:01,235 and then we return y, so, 42 00:03:01,235 --> 00:03:08,125 the value of this entire expression square of toSquare becomes 100, 43 00:03:08,125 --> 00:03:13,940 and that is the value that gets assigned to the variable square result. 44 00:03:13,970 --> 00:03:17,200 Now, if you were paying close attention there, 45 00:03:17,200 --> 00:03:20,650 you will have seen that when we finish the execution of the square function, 46 00:03:20,650 --> 00:03:24,500 some of the variables disappeared over here, 47 00:03:24,720 --> 00:03:29,635 we had them, and then they disappeared when we finished. 48 00:03:29,635 --> 00:03:33,025 We're going to talk more about that in later screen casts. 49 00:03:33,025 --> 00:03:36,305 So hold on for some of those details. 50 00:03:36,305 --> 00:03:40,160 Now, if a function doesn't have a return statement, 51 00:03:40,160 --> 00:03:45,930 it will automatically return a special value called 'none'. 52 00:03:46,480 --> 00:03:53,615 Let's see that, here's an example illustrating a common confusion for students, 53 00:03:53,615 --> 00:03:57,750 printing a value doesn't return that value, 54 00:03:58,700 --> 00:04:01,240 so on line three, 55 00:04:01,240 --> 00:04:04,980 we really should have a return statement. 56 00:04:05,450 --> 00:04:08,295 In our definition of the square function, 57 00:04:08,295 --> 00:04:13,190 we've calculated 10 times 10 or whatever it is and we should be returning it, 58 00:04:13,190 --> 00:04:15,480 but instead we're printing it. 59 00:04:19,540 --> 00:04:23,210 If we don't have a return statement at all, 60 00:04:23,210 --> 00:04:24,940 in the function definition it says, 61 00:04:24,940 --> 00:04:32,950 "If we had a statement at the end saying return the special value none." 62 00:04:32,950 --> 00:04:37,650 So, let's see what's going to happen in this execution, 63 00:04:38,600 --> 00:04:41,235 we define the function square, 64 00:04:41,235 --> 00:04:44,370 we set toSquare to have the value 10. 65 00:04:44,370 --> 00:04:46,905 Now we invoke the function, 66 00:04:46,905 --> 00:04:50,470 x gets as its value. 67 00:04:50,470 --> 00:04:57,860 Now, behind the scenes assignment x is getting whatever toSquare has, 68 00:04:57,860 --> 00:05:02,000 so x gets the 10 toSquare was, 69 00:05:02,000 --> 00:05:04,415 x has the value 10, 70 00:05:04,415 --> 00:05:10,530 and we execute y gets the value of a 100, 71 00:05:10,530 --> 00:05:13,710 and then we print y, so, 72 00:05:13,710 --> 00:05:17,190 we can see in the output window, we've already printed y. 73 00:05:17,190 --> 00:05:24,050 Well, we didn't return anything and so it's as if we have returned the value none, 74 00:05:24,050 --> 00:05:30,580 and that means that square result is going to have as its value, none. 75 00:05:33,190 --> 00:05:39,090 Now, you see here square result has the value none, 76 00:05:39,090 --> 00:05:43,800 and that means that when we go to print, this formatted statement, 77 00:05:43,800 --> 00:05:47,440 the result of something squared is 78 00:05:47,440 --> 00:05:51,730 something we're going to get that the result of 10 squared is none, 79 00:05:51,730 --> 00:05:55,430 rather than the result of 10 squared being 100. 80 00:05:57,890 --> 00:06:05,840 What we really needed to do was not print that return y, 81 00:06:06,390 --> 00:06:11,930 and then we would have gotten that the result of 10 squared is 100. 82 00:06:12,170 --> 00:06:15,820 One other important thing about the return statement, 83 00:06:15,820 --> 00:06:19,150 it interrupts the execution of the function, 84 00:06:19,150 --> 00:06:24,605 no other code in the function executes after a return statement is executed. 85 00:06:24,605 --> 00:06:27,164 If you've played the board game Monopoly, 86 00:06:27,164 --> 00:06:29,020 it's like the go to jail move. 87 00:06:29,020 --> 00:06:32,095 Do not pass gold, do not collect $200. 88 00:06:32,095 --> 00:06:35,740 In our case, do not execute any more code inside 89 00:06:35,740 --> 00:06:39,640 this function even if you're in the middle of a four-loop or an if statement, 90 00:06:39,640 --> 00:06:44,230 return statement makes you skip the rest of the code in that function. 91 00:06:44,230 --> 00:06:50,535 Here's an example, define this function weird, 92 00:06:50,535 --> 00:06:53,260 not actually useful, but it's going to be very 93 00:06:53,260 --> 00:06:57,190 illustrative of how this immediate return works. 94 00:06:57,190 --> 00:07:00,295 So, lines one through five to find the function, 95 00:07:00,295 --> 00:07:04,080 line six invokes it so when we invoke it, 96 00:07:04,080 --> 00:07:06,460 and we get to line two, 97 00:07:08,810 --> 00:07:12,255 we'll print out the word here, 98 00:07:12,255 --> 00:07:14,910 and then we'll return the value five. 99 00:07:14,910 --> 00:07:17,435 As soon as we return, 100 00:07:17,435 --> 00:07:22,210 means we are not going to do anything with the remaining lines of code, 101 00:07:22,210 --> 00:07:25,180 they are orphaned, we will not pass go, 102 00:07:25,180 --> 00:07:28,760 we will not execute print there, 103 00:07:28,920 --> 00:07:34,450 we will return the value five and x will have the value five, 104 00:07:34,450 --> 00:07:37,640 and we will print five. 105 00:07:40,380 --> 00:07:47,225 So, we do get the here printing, 106 00:07:47,225 --> 00:07:49,965 we don't get there, 107 00:07:49,965 --> 00:07:52,400 there never shows up in our output, 108 00:07:52,400 --> 00:07:57,080 and the five is coming from line eight. 109 00:07:58,910 --> 00:08:03,535 That's not a very useful function which is why I called it weird, 110 00:08:03,535 --> 00:08:06,730 but let's look at a common programming pattern 111 00:08:06,730 --> 00:08:09,790 where return does occur in the middle of a function, 112 00:08:09,790 --> 00:08:11,750 and it's a good idea. 113 00:08:11,750 --> 00:08:18,125 This function called longer than five returns a Boolean value, true or false? 114 00:08:18,125 --> 00:08:20,520 It takes as an input, 115 00:08:20,520 --> 00:08:24,350 it's one formal parameter called lists of names, 116 00:08:25,020 --> 00:08:29,220 it's expecting there to be a list of strings. 117 00:08:29,220 --> 00:08:32,735 If any of those strings has more than five characters, 118 00:08:32,735 --> 00:08:35,140 the function returns true, 119 00:08:35,140 --> 00:08:38,050 and otherwise it returns false. 120 00:08:38,050 --> 00:08:41,865 So with list one, 121 00:08:41,865 --> 00:08:45,285 all the name Sam, Tera, Sal, Amita, 122 00:08:45,285 --> 00:08:48,400 they're all five characters or less returned 123 00:08:48,400 --> 00:08:52,975 false because none of them have a name longer than five characters. 124 00:08:52,975 --> 00:08:57,760 With lists two, both Lauren and Natalie are names that 125 00:08:57,760 --> 00:09:03,620 have more than five characters and so longer than five will return true. 126 00:09:03,630 --> 00:09:09,655 How does it work? Well, we loop through all of the names. 127 00:09:09,655 --> 00:09:14,890 That's what's happening on lines two through five, 128 00:09:16,310 --> 00:09:19,290 and if we ever get a long one, 129 00:09:19,290 --> 00:09:22,080 if the current name is long, 130 00:09:22,080 --> 00:09:24,915 then we will execute line four, 131 00:09:24,915 --> 00:09:27,660 and we will return the value true. 132 00:09:27,660 --> 00:09:29,835 As soon as we do that, 133 00:09:29,835 --> 00:09:31,670 we're done with the four loop, 134 00:09:31,670 --> 00:09:33,220 we're done with everything. 135 00:09:33,220 --> 00:09:36,025 Do not pass go, don't collect $200, 136 00:09:36,025 --> 00:09:37,930 get out of this function, 137 00:09:37,930 --> 00:09:40,910 and just return the value true. 138 00:09:42,500 --> 00:09:47,020 If however, we managed to get through the entire iteration 139 00:09:47,020 --> 00:09:51,250 without ever exiting the function, 140 00:09:51,250 --> 00:09:53,500 without ever executing line four, 141 00:09:53,500 --> 00:09:58,830 then we will execute line six which says to return false. 142 00:09:58,830 --> 00:10:03,260 So, the reason this logic works is that we're never going to get to 143 00:10:03,260 --> 00:10:08,510 line six if any of the names are long. 144 00:10:08,510 --> 00:10:13,010 We'll only get to line six if we've processed all of the names, 145 00:10:13,010 --> 00:10:16,730 and none of them caused us to return true. 146 00:10:16,730 --> 00:10:20,225 So, that means that none of the names were long, 147 00:10:20,225 --> 00:10:23,490 and it's safe to return false. 148 00:10:24,970 --> 00:10:31,290 Let's see this in code lens, we'll go step-by-step. 149 00:10:32,510 --> 00:10:35,080 So, we define this function, 150 00:10:35,080 --> 00:10:38,495 we invoke it the first time on the list one, 151 00:10:38,495 --> 00:10:40,600 Sam, Tera, Sal, and Amita. 152 00:10:40,600 --> 00:10:42,970 So behind the scenes, 153 00:10:42,970 --> 00:10:51,700 we get list of names getting bound to that list, 154 00:10:52,410 --> 00:10:56,230 and then we're going to start iterating through that list 155 00:10:56,230 --> 00:10:59,110 of names and the first iteration, 156 00:10:59,110 --> 00:11:01,720 our variable name is bound to Sam, 157 00:11:01,720 --> 00:11:04,195 is it longer than five characters? 158 00:11:04,195 --> 00:11:09,280 No, it's not. So, we go on and get a new value for name, 159 00:11:09,280 --> 00:11:11,450 name is now bound to Tera, 160 00:11:11,450 --> 00:11:13,425 Tera is not long, 161 00:11:13,425 --> 00:11:15,420 Sal is not long, 162 00:11:15,420 --> 00:11:17,630 Amita is also not long, 163 00:11:17,630 --> 00:11:20,500 and now we've iterated through all the list of 164 00:11:20,500 --> 00:11:24,650 names and we get to line six where it says, 165 00:11:24,650 --> 00:11:33,800 "I guess none of them are long so let's return false" The second time we invoke it, 166 00:11:33,800 --> 00:11:37,985 we get the other list of names and that when we iterate through, 167 00:11:37,985 --> 00:11:40,055 Rey is not longer than five, 168 00:11:40,055 --> 00:11:41,990 Ayo is not longer than five, 169 00:11:41,990 --> 00:11:44,270 but Lauren is longer than five, 170 00:11:44,270 --> 00:11:46,800 and so finally line four executes, 171 00:11:46,800 --> 00:11:50,885 we return true, we never even get to look at Natalie. 172 00:11:50,885 --> 00:11:56,045 But Lauren was long, and that's enough to know that one of them was too long, 173 00:11:56,045 --> 00:11:58,685 and we get an answer of true. 174 00:11:58,685 --> 00:12:03,150 So in summary, to return a value from a function, 175 00:12:03,150 --> 00:12:10,180 we have the word return and then some expression that evaluates to a value. 176 00:12:10,180 --> 00:12:12,550 In this case, the value false. 177 00:12:12,550 --> 00:12:14,815 If we're in the middle of the function, 178 00:12:14,815 --> 00:12:16,390 and we have a return, 179 00:12:16,390 --> 00:12:19,060 we ignore the rest of the code in the function, 180 00:12:19,060 --> 00:12:20,500 we exit out of the four-loop, 181 00:12:20,500 --> 00:12:21,950 we exit out of the if, 182 00:12:21,950 --> 00:12:24,390 we don't pass go, we don't collect $200, 183 00:12:24,390 --> 00:12:26,415 we're just done with this function. 184 00:12:26,415 --> 00:12:32,740 If, we have a function that never executes any return statement at all, 185 00:12:32,740 --> 00:12:35,315 we will return the special value none. 186 00:12:35,315 --> 00:12:40,540 The other thing to remember is that print is for people, 187 00:12:40,540 --> 00:12:45,220 you can use it inside of a function and it'll generate an output in the output window, 188 00:12:45,220 --> 00:12:48,335 but it doesn't cause anything to return from the function. 189 00:12:48,335 --> 00:12:50,610 Return is for functions, 190 00:12:50,610 --> 00:12:52,240 it doesn't print anything, 191 00:12:52,240 --> 00:12:59,130 but it returns a value to the spot where the function was invoked. See you next time.