WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:02.910 [MUSIC PLAYING] 00:00:05.340 --> 00:00:07.740 SPEAKER: OK, so we've seen so far that functions 00:00:07.740 --> 00:00:12.570 are these building blocks of programs that take input and produce output. 00:00:12.570 --> 00:00:15.240 Now, it turns out as we look more closely, 00:00:15.240 --> 00:00:18.270 functions can produce two kinds of outputs. 00:00:18.270 --> 00:00:20.760 One kind is a return value. 00:00:20.760 --> 00:00:22.890 And one kind is a side effect. 00:00:22.890 --> 00:00:25.955 So in this short, we'll look more closely at return values 00:00:25.955 --> 00:00:28.830 and what they can do to make our programs more flexible as you go off 00:00:28.830 --> 00:00:31.830 and write more complex programs over time. 00:00:31.830 --> 00:00:36.900 Now, here, I have a function called Greet, where Greet takes an input, 00:00:36.900 --> 00:00:38.550 as you can see on line one. 00:00:38.550 --> 00:00:40.860 Now, the function does something very simple. 00:00:40.860 --> 00:00:43.770 It basically asks the question, is "hello," 00:00:43.770 --> 00:00:46.020 this text "hello," is that in the input? 00:00:46.020 --> 00:00:50.670 And if it is, it's going to print on the terminal "hello, there" in response. 00:00:50.670 --> 00:00:53.430 Otherwise, it's going to say "I'm not sure what you mean." 00:00:53.430 --> 00:00:55.380 So it is if "hello" is not in the input, it's 00:00:55.380 --> 00:00:58.255 going to say "I'm not sure what you're talking about," which is fine. 00:00:58.255 --> 00:01:00.840 This is a very simple chat bot, if you will. 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 00:01:05.440 --> 00:01:07.270 to call it with some input. 00:01:07.270 --> 00:01:10.990 So I'll type Greet down below and I'll say hello to the computer. 00:01:10.990 --> 00:01:13.420 I'll say "hello, computer." 00:01:13.420 --> 00:01:14.140 I'll save it. 00:01:14.140 --> 00:01:18.640 And I'll say python of greeting.py in my terminal, hit Enter, 00:01:18.640 --> 00:01:21.940 and now I should see "hello, there," which is really nice. 00:01:21.940 --> 00:01:25.600 So this is an example of a side effect. 00:01:25.600 --> 00:01:27.880 I'm seeing something in my terminal. 00:01:27.880 --> 00:01:30.760 But a return value is slightly different. 00:01:30.760 --> 00:01:35.230 A return value is some value a function can pass to my program 00:01:35.230 --> 00:01:37.930 to use later on in my code. 00:01:37.930 --> 00:01:40.990 And actually, right now, I haven't explicitly 00:01:40.990 --> 00:01:43.780 said that Greet returns anything. 00:01:43.780 --> 00:01:48.340 To do that, I need to use this keyword called Return in Python. 00:01:48.340 --> 00:01:50.770 So instead of printing here where printing 00:01:50.770 --> 00:01:53.710 produces some side effect of text on the screen, 00:01:53.710 --> 00:01:55.960 let me try returning the actual text. 00:01:55.960 --> 00:01:58.810 I'll say why don't I return "hello, there" 00:01:58.810 --> 00:02:02.740 and why don't I return "I'm not sure what you mean"? 00:02:02.740 --> 00:02:07.810 So with this change, now I've said that Greet has an explicitly-defined return 00:02:07.810 --> 00:02:08.440 value. 00:02:08.440 --> 00:02:12.460 If hello is in the input, the return value will be "hello, there." 00:02:12.460 --> 00:02:15.820 If "hello" is not in the input, the return value will be 00:02:15.820 --> 00:02:17.920 "I'm not sure what you mean." 00:02:17.920 --> 00:02:19.060 So let's try this again. 00:02:19.060 --> 00:02:23.710 I'll say python of greeting.py, and I'll hit enter. 00:02:23.710 --> 00:02:27.370 And I don't see anything anymore, but that doesn't mean things are broken. 00:02:27.370 --> 00:02:32.170 So what happens now is that Greet is returning some value, 00:02:32.170 --> 00:02:36.310 but I'm not really capturing it or using it in my program yet. 00:02:36.310 --> 00:02:38.650 To capture it and to use it later on, I need 00:02:38.650 --> 00:02:42.700 to make sure I store the return value inside of some variable. 00:02:42.700 --> 00:02:47.410 And I can do that, let's say, in a variable called Greeting. 00:02:47.410 --> 00:02:51.250 So I'll say Greeting equals the result, or the return 00:02:51.250 --> 00:02:56.050 value of calling Greet with this input called "hello, computer." 00:02:56.050 --> 00:03:00.250 So now line-by-line what will happen is I will first run Greet 00:03:00.250 --> 00:03:02.620 and I'll give the input "hello, computer." 00:03:02.620 --> 00:03:07.420 "Hello" is in that input, so Greet will return to me the value "hello, 00:03:07.420 --> 00:03:10.960 there" and it will then store it using this Greeting 00:03:10.960 --> 00:03:13.510 variable in this assignment operator, this equal sign 00:03:13.510 --> 00:03:16.690 here, inside this variable called Greeting. 00:03:16.690 --> 00:03:21.040 And now, I could actually go ahead and print the greeting itself. 00:03:21.040 --> 00:03:23.560 So on line nine here, I'll print Greeting. 00:03:23.560 --> 00:03:24.580 Now, I'll try this. 00:03:24.580 --> 00:03:28.270 I'll say python of greeting.py and I'll hit Enter, 00:03:28.270 --> 00:03:29.830 and now I see "hello, there." 00:03:29.830 --> 00:03:35.380 What if I did something like, how's the weather, like this? 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." 00:03:40.840 --> 00:03:42.310 So, kind of nice. 00:03:42.310 --> 00:03:44.500 Now I'll say "hello, computer" again. 00:03:44.500 --> 00:03:50.410 And notice I can take this variable and modify it in all kinds of ways. 00:03:50.410 --> 00:03:54.520 Before, when I just had these simple print statements in Greet, 00:03:54.520 --> 00:03:59.080 going back to something like this, print "hello there" and print 00:03:59.080 --> 00:04:02.710 "I'm not sure what you mean," those were the only things I could print. 00:04:02.710 --> 00:04:05.920 But now with the return value, I get access 00:04:05.920 --> 00:04:11.020 to all kinds of ways I could modify the return value later on in my code. 00:04:11.020 --> 00:04:13.930 Maybe I could do something like this. 00:04:13.930 --> 00:04:15.610 If maybe I could say-- 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" 00:04:20.470 --> 00:04:22.480 and then add the greeting on later. 00:04:22.480 --> 00:04:26.170 So now everything will begin with "hm" followed by the greeting. 00:04:26.170 --> 00:04:31.900 So I'll say python greeting.py, and now it should say "hm, hello, there." 00:04:31.900 --> 00:04:35.170 Or, I could say something like "how's the weather?" 00:04:35.170 --> 00:04:36.370 "How's the weather." 00:04:36.370 --> 00:04:39.985 Now I could say python greeting.py, and it's a little more thoughtful. 00:04:39.985 --> 00:04:42.640 "Hm, I'm not sure what you mean." 00:04:42.640 --> 00:04:44.380 And I could even go further. 00:04:44.380 --> 00:04:46.210 I could say "hello, computer" again. 00:04:46.210 --> 00:04:48.880 And why don't I try to have different kinds of greetings? 00:04:48.880 --> 00:04:55.270 I could say a greeting and then I could say, space, "Carter," just like this. 00:04:55.270 --> 00:04:58.900 And now I could say python of greeting.py, and it says, oh, "hello, 00:04:58.900 --> 00:04:59.800 there, Carter." 00:04:59.800 --> 00:05:05.010 Or, I could do greeting plus David, and now it says python greeting.py, 00:05:05.010 --> 00:05:08.340 and then "hello, there, David," or Rongxin. 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 00:05:12.150 --> 00:05:13.200 at the end. 00:05:13.200 --> 00:05:17.190 So as you can see, the value of return value 00:05:17.190 --> 00:05:20.670 is that it allows us to modify things throughout our code 00:05:20.670 --> 00:05:24.210 and to use the output of some function later on. 00:05:24.210 --> 00:05:26.310 A side effect is slightly different. 00:05:26.310 --> 00:05:29.920 It simply produces some change immediately, whereas a return value, 00:05:29.920 --> 00:05:34.460 you can use throughout our code as you go off and build new programs.