1 00:00:00,000 --> 00:00:02,910 [MUSIC PLAYING] 2 00:00:02,910 --> 00:00:05,340 3 00:00:05,340 --> 00:00:07,740 SPEAKER: OK, so we've seen so far that functions 4 00:00:07,740 --> 00:00:12,570 are these building blocks of programs that take input and produce output. 5 00:00:12,570 --> 00:00:15,240 Now, it turns out as we look more closely, 6 00:00:15,240 --> 00:00:18,270 functions can produce two kinds of outputs. 7 00:00:18,270 --> 00:00:20,760 One kind is a return value. 8 00:00:20,760 --> 00:00:22,890 And one kind is a side effect. 9 00:00:22,890 --> 00:00:25,955 So in this short, we'll look more closely at return values 10 00:00:25,955 --> 00:00:28,830 and what they can do to make our programs more flexible as you go off 11 00:00:28,830 --> 00:00:31,830 and write more complex programs over time. 12 00:00:31,830 --> 00:00:36,900 Now, here, I have a function called Greet, where Greet takes an input, 13 00:00:36,900 --> 00:00:38,550 as you can see on line one. 14 00:00:38,550 --> 00:00:40,860 Now, the function does something very simple. 15 00:00:40,860 --> 00:00:43,770 It basically asks the question, is "hello," 16 00:00:43,770 --> 00:00:46,020 this text "hello," is that in the input? 17 00:00:46,020 --> 00:00:50,670 And if it is, it's going to print on the terminal "hello, there" in response. 18 00:00:50,670 --> 00:00:53,430 Otherwise, it's going to say "I'm not sure what you mean." 19 00:00:53,430 --> 00:00:55,380 So it is if "hello" is not in the input, it's 20 00:00:55,380 --> 00:00:58,255 going to say "I'm not sure what you're talking about," which is fine. 21 00:00:58,255 --> 00:01:00,840 This is a very simple chat bot, if you will. 22 00:01:00,840 --> 00:01:05,440 So I'll go down to line eight and let me try to actually run this function 23 00:01:05,440 --> 00:01:07,270 to call it with some input. 24 00:01:07,270 --> 00:01:10,990 So I'll type Greet down below and I'll say hello to the computer. 25 00:01:10,990 --> 00:01:13,420 I'll say "hello, computer." 26 00:01:13,420 --> 00:01:14,140 I'll save it. 27 00:01:14,140 --> 00:01:18,640 And I'll say python of greeting.py in my terminal, hit Enter, 28 00:01:18,640 --> 00:01:21,940 and now I should see "hello, there," which is really nice. 29 00:01:21,940 --> 00:01:25,600 So this is an example of a side effect. 30 00:01:25,600 --> 00:01:27,880 I'm seeing something in my terminal. 31 00:01:27,880 --> 00:01:30,760 But a return value is slightly different. 32 00:01:30,760 --> 00:01:35,230 A return value is some value a function can pass to my program 33 00:01:35,230 --> 00:01:37,930 to use later on in my code. 34 00:01:37,930 --> 00:01:40,990 And actually, right now, I haven't explicitly 35 00:01:40,990 --> 00:01:43,780 said that Greet returns anything. 36 00:01:43,780 --> 00:01:48,340 To do that, I need to use this keyword called Return in Python. 37 00:01:48,340 --> 00:01:50,770 So instead of printing here where printing 38 00:01:50,770 --> 00:01:53,710 produces some side effect of text on the screen, 39 00:01:53,710 --> 00:01:55,960 let me try returning the actual text. 40 00:01:55,960 --> 00:01:58,810 I'll say why don't I return "hello, there" 41 00:01:58,810 --> 00:02:02,740 and why don't I return "I'm not sure what you mean"? 42 00:02:02,740 --> 00:02:07,810 So with this change, now I've said that Greet has an explicitly-defined return 43 00:02:07,810 --> 00:02:08,440 value. 44 00:02:08,440 --> 00:02:12,460 If hello is in the input, the return value will be "hello, there." 45 00:02:12,460 --> 00:02:15,820 If "hello" is not in the input, the return value will be 46 00:02:15,820 --> 00:02:17,920 "I'm not sure what you mean." 47 00:02:17,920 --> 00:02:19,060 So let's try this again. 48 00:02:19,060 --> 00:02:23,710 I'll say python of greeting.py, and I'll hit enter. 49 00:02:23,710 --> 00:02:27,370 And I don't see anything anymore, but that doesn't mean things are broken. 50 00:02:27,370 --> 00:02:32,170 So what happens now is that Greet is returning some value, 51 00:02:32,170 --> 00:02:36,310 but I'm not really capturing it or using it in my program yet. 52 00:02:36,310 --> 00:02:38,650 To capture it and to use it later on, I need 53 00:02:38,650 --> 00:02:42,700 to make sure I store the return value inside of some variable. 54 00:02:42,700 --> 00:02:47,410 And I can do that, let's say, in a variable called Greeting. 55 00:02:47,410 --> 00:02:51,250 So I'll say Greeting equals the result, or the return 56 00:02:51,250 --> 00:02:56,050 value of calling Greet with this input called "hello, computer." 57 00:02:56,050 --> 00:03:00,250 So now line-by-line what will happen is I will first run Greet 58 00:03:00,250 --> 00:03:02,620 and I'll give the input "hello, computer." 59 00:03:02,620 --> 00:03:07,420 "Hello" is in that input, so Greet will return to me the value "hello, 60 00:03:07,420 --> 00:03:10,960 there" and it will then store it using this Greeting 61 00:03:10,960 --> 00:03:13,510 variable in this assignment operator, this equal sign 62 00:03:13,510 --> 00:03:16,690 here, inside this variable called Greeting. 63 00:03:16,690 --> 00:03:21,040 And now, I could actually go ahead and print the greeting itself. 64 00:03:21,040 --> 00:03:23,560 So on line nine here, I'll print Greeting. 65 00:03:23,560 --> 00:03:24,580 Now, I'll try this. 66 00:03:24,580 --> 00:03:28,270 I'll say python of greeting.py and I'll hit Enter, 67 00:03:28,270 --> 00:03:29,830 and now I see "hello, there." 68 00:03:29,830 --> 00:03:35,380 What if I did something like, how's the weather, like this? 69 00:03:35,380 --> 00:03:40,840 I'll say python of greeting.py, and now I see "I'm not sure what you mean." 70 00:03:40,840 --> 00:03:42,310 So, kind of nice. 71 00:03:42,310 --> 00:03:44,500 Now I'll say "hello, computer" again. 72 00:03:44,500 --> 00:03:50,410 And notice I can take this variable and modify it in all kinds of ways. 73 00:03:50,410 --> 00:03:54,520 Before, when I just had these simple print statements in Greet, 74 00:03:54,520 --> 00:03:59,080 going back to something like this, print "hello there" and print 75 00:03:59,080 --> 00:04:02,710 "I'm not sure what you mean," those were the only things I could print. 76 00:04:02,710 --> 00:04:05,920 But now with the return value, I get access 77 00:04:05,920 --> 00:04:11,020 to all kinds of ways I could modify the return value later on in my code. 78 00:04:11,020 --> 00:04:13,930 Maybe I could do something like this. 79 00:04:13,930 --> 00:04:15,610 If maybe I could say-- 80 00:04:15,610 --> 00:04:20,470 why don't I print not just Greeting, but why don't I print something like "hm" 81 00:04:20,470 --> 00:04:22,480 and then add the greeting on later. 82 00:04:22,480 --> 00:04:26,170 So now everything will begin with "hm" followed by the greeting. 83 00:04:26,170 --> 00:04:31,900 So I'll say python greeting.py, and now it should say "hm, hello, there." 84 00:04:31,900 --> 00:04:35,170 Or, I could say something like "how's the weather?" 85 00:04:35,170 --> 00:04:36,370 "How's the weather." 86 00:04:36,370 --> 00:04:39,985 Now I could say python greeting.py, and it's a little more thoughtful. 87 00:04:39,985 --> 00:04:42,640 "Hm, I'm not sure what you mean." 88 00:04:42,640 --> 00:04:44,380 And I could even go further. 89 00:04:44,380 --> 00:04:46,210 I could say "hello, computer" again. 90 00:04:46,210 --> 00:04:48,880 And why don't I try to have different kinds of greetings? 91 00:04:48,880 --> 00:04:55,270 I could say a greeting and then I could say, space, "Carter," just like this. 92 00:04:55,270 --> 00:04:58,900 And now I could say python of greeting.py, and it says, oh, "hello, 93 00:04:58,900 --> 00:04:59,800 there, Carter." 94 00:04:59,800 --> 00:05:05,010 Or, I could do greeting plus David, and now it says python greeting.py, 95 00:05:05,010 --> 00:05:08,340 and then "hello, there, David," or Rongxin. 96 00:05:08,340 --> 00:05:12,150 I could type any name after this and I could kind of add on people's names 97 00:05:12,150 --> 00:05:13,200 at the end. 98 00:05:13,200 --> 00:05:17,190 So as you can see, the value of return value 99 00:05:17,190 --> 00:05:20,670 is that it allows us to modify things throughout our code 100 00:05:20,670 --> 00:05:24,210 and to use the output of some function later on. 101 00:05:24,210 --> 00:05:26,310 A side effect is slightly different. 102 00:05:26,310 --> 00:05:29,920 It simply produces some change immediately, whereas a return value, 103 00:05:29,920 --> 00:05:34,460 you can use throughout our code as you go off and build new programs. 104 00:05:34,460 --> 00:05:36,000