1 00:00:00,385 --> 00:00:01,123 All right. 2 00:00:01,123 --> 00:00:05,598 Now that we've recapped what we know about functions, let's get to the next concept, 3 00:00:05,598 --> 00:00:07,252 getting data out of functions. 4 00:00:07,252 --> 00:00:11,568 Since the code in a global context can't access what's in a local context, 5 00:00:11,568 --> 00:00:14,360 functions might seem pretty limiting. 6 00:00:14,360 --> 00:00:18,350 What's the point if what happens inside them stays inside them? 7 00:00:18,350 --> 00:00:22,650 Well, programmers have a solution for this, it's called returning. 8 00:00:22,650 --> 00:00:26,180 Returning a value from a function is extremely common, and 9 00:00:26,180 --> 00:00:31,060 it's a tool you'll use all the time, and you only need to know one word, return. 10 00:00:31,060 --> 00:00:34,816 Let's look at an example, feel free to just watch and follow along with my 11 00:00:34,816 --> 00:00:38,347 workspace, you'll have an opportunity to try on your own shortly. 12 00:00:38,347 --> 00:00:41,870 So here we have a function called two_plus_two. 13 00:00:41,870 --> 00:00:45,030 Like the name suggests, this function adds 2 + 2, 14 00:00:45,030 --> 00:00:47,950 and assigns the value to a variable called val. 15 00:00:49,160 --> 00:00:51,056 Let's call the function and run the program. 16 00:01:02,819 --> 00:01:04,205 Well, that's weird. 17 00:01:04,205 --> 00:01:07,283 It doesn't look like much if anything has happened. 18 00:01:07,283 --> 00:01:11,125 The variable val was created and assigned a value in the background, but 19 00:01:11,125 --> 00:01:14,945 we didn't do anything with it, so we can't see or use that value. 20 00:01:14,945 --> 00:01:16,975 And none of this was really very helpful to us. 21 00:01:18,020 --> 00:01:21,340 Okay, so let's try returning the variable from the function so 22 00:01:21,340 --> 00:01:25,190 we can access it outside the function in our global context. 23 00:01:25,190 --> 00:01:28,360 To do that, I'll go back to our function. 24 00:01:28,360 --> 00:01:29,660 Create a new line, and 25 00:01:29,660 --> 00:01:34,070 I'll use the keyword return followed by the value I'm returning. 26 00:01:34,070 --> 00:01:38,000 Now, you can return a specific value like a string or an integer, or 27 00:01:38,000 --> 00:01:39,770 you can return a variable. 28 00:01:39,770 --> 00:01:41,320 When you return a variable, 29 00:01:41,320 --> 00:01:44,340 the value assigned to the variable is what gets returned. 30 00:01:44,340 --> 00:01:47,000 So in this case, our function will be returning the integer 4, but 31 00:01:47,000 --> 00:01:49,480 we’re going to write return val. 32 00:01:50,860 --> 00:01:53,801 Now, I’m going to save this and try to run it again. 33 00:01:57,517 --> 00:02:01,030 Wow, that’s kind of weird, it still doesn't look like anything happened. 34 00:02:01,030 --> 00:02:05,020 But actually that makes sense because the value was returned from the function, but 35 00:02:05,020 --> 00:02:06,470 we still didn't do anything with it. 36 00:02:07,710 --> 00:02:08,470 So let's break this down. 37 00:02:09,530 --> 00:02:12,000 When you call a function that returns the value, 38 00:02:12,000 --> 00:02:14,710 the return value is sent back to the function call. 39 00:02:15,710 --> 00:02:19,270 The function call now behaves in the same way a variable might. 40 00:02:19,270 --> 00:02:23,030 It's holding a value that can be printed, assigned to another variable, or 41 00:02:23,030 --> 00:02:24,290 used in an expression. 42 00:02:24,290 --> 00:02:26,170 So we're going to try all of these. 43 00:02:26,170 --> 00:02:27,293 First, I'll print it. 44 00:02:32,387 --> 00:02:35,779 Now I'll save and run again. 45 00:02:35,779 --> 00:02:38,800 Cool, it prints out 4, which is the value returned from the function. 46 00:02:39,920 --> 00:02:44,106 But a more common way to handle function returns is to assign them to a variable, 47 00:02:44,106 --> 00:02:45,305 that looks like this. 48 00:02:53,301 --> 00:02:58,611 So, in this example, I've created a variable called sum, and the value 49 00:02:58,611 --> 00:03:04,560 that we're assigning to sum is the return value from the call to two_plus_two. 50 00:03:04,560 --> 00:03:07,640 To break that down further, when the Python interpreter processes 51 00:03:07,640 --> 00:03:11,770 the right-hand side of the statement, it calls the two_plus_two function, 52 00:03:11,770 --> 00:03:16,128 which then calculates the value of 2 + 2 and returns it back. 53 00:03:16,128 --> 00:03:18,640 The interpreter then takes that returned value and 54 00:03:18,640 --> 00:03:20,400 assigns it to the new variable, sum. 55 00:03:21,550 --> 00:03:23,901 Let's print out the value of sum to demonstrate this. 56 00:03:29,330 --> 00:03:30,332 Now save and run. 57 00:03:34,734 --> 00:03:36,170 This also prints out 4. 58 00:03:36,170 --> 00:03:39,530 And as you can see, printing the function call is equivalent 59 00:03:39,530 --> 00:03:42,810 to assigning the function call to a variable and printing that variable. 60 00:03:44,130 --> 00:03:48,050 Now finally, we can use the function call in an expression. 61 00:03:48,050 --> 00:03:50,050 Our function is returning an integer. 62 00:03:50,050 --> 00:03:53,475 So let's try to take that value and multiply it by two. 63 00:03:53,475 --> 00:03:55,890 How about you give this a try on your own? 64 00:03:55,890 --> 00:03:59,230 Pause the video here and open up the attached workspace. 65 00:03:59,230 --> 00:04:02,860 Add a return statement inside the two_plus_two function 66 00:04:02,860 --> 00:04:04,260 that returns the val variable. 67 00:04:05,310 --> 00:04:10,140 After the function, print out the product of the return value and the number 2. 68 00:04:10,140 --> 00:04:13,140 When you're done, un-pause the video to see the solution. 69 00:04:14,560 --> 00:04:16,066 Okay, how did it go? 70 00:04:16,066 --> 00:04:19,780 To use the function call in an expression, pretend it's a regular integer or 71 00:04:19,780 --> 00:04:21,180 variable. 72 00:04:21,180 --> 00:04:25,504 So, in the following example expression, 4 * 2, 73 00:04:25,504 --> 00:04:29,165 we can replace the 4 with the function call. 74 00:04:33,708 --> 00:04:34,861 Then we can print it out. 75 00:04:40,539 --> 00:04:42,138 So we'll save and run. 76 00:04:45,500 --> 00:04:48,590 Awesome, this prints out 8, just as expected. 77 00:04:48,590 --> 00:04:51,550 That's because the return value from the function is 4 and 78 00:04:51,550 --> 00:04:54,490 we multiply that by 2 in our print statement. 79 00:04:54,490 --> 00:04:55,475 Great work. 80 00:04:55,475 --> 00:04:58,660 In the next video, we'll expand on our function discussion to show more about how 81 00:04:58,660 --> 00:05:01,390 useful they can be and why we'd want to use them. 82 00:05:01,390 --> 00:05:02,930 See you soon and keep on coding.