All About Returns5:03 with Ashley Boucher
How to get data out of functions using returns.
All right. 0:00 Now that we've recapped what we know about functions, let's get to the next concept, 0:01 getting data out of functions. 0:05 Since the code in a global context can't access what's in a local context, 0:07 functions might seem pretty limiting. 0:11 What's the point if what happens inside them stays inside them? 0:14 Well, programmers have a solution for this, it's called returning. 0:18 Returning a value from a function is extremely common, and 0:22 it's a tool you'll use all the time, and you only need to know one word, return. 0:26 Let's look at an example, feel free to just watch and follow along with my 0:31 workspace, you'll have an opportunity to try on your own shortly. 0:34 So here we have a function called two_plus_two. 0:38 Like the name suggests, this function adds 2 + 2, 0:41 and assigns the value to a variable called val. 0:45 Let's call the function and run the program. 0:49 Well, that's weird. 1:02 It doesn't look like much if anything has happened. 1:04 The variable val was created and assigned a value in the background, but 1:07 we didn't do anything with it, so we can't see or use that value. 1:11 And none of this was really very helpful to us. 1:14 Okay, so let's try returning the variable from the function so 1:18 we can access it outside the function in our global context. 1:21 To do that, I'll go back to our function. 1:25 Create a new line, and 1:28 I'll use the keyword return followed by the value I'm returning. 1:29 Now, you can return a specific value like a string or an integer, or 1:34 you can return a variable. 1:38 When you return a variable, 1:39 the value assigned to the variable is what gets returned. 1:41 So in this case, our function will be returning the integer 4, but 1:44 we’re going to write return val. 1:47 Now, I’m going to save this and try to run it again. 1:50 Wow, that’s kind of weird, it still doesn't look like anything happened. 1:57 But actually that makes sense because the value was returned from the function, but 2:01 we still didn't do anything with it. 2:05 So let's break this down. 2:07 When you call a function that returns the value, 2:09 the return value is sent back to the function call. 2:12 The function call now behaves in the same way a variable might. 2:15 It's holding a value that can be printed, assigned to another variable, or 2:19 used in an expression. 2:23 So we're going to try all of these. 2:24 First, I'll print it. 2:26 Now I'll save and run again. 2:32 Cool, it prints out 4, which is the value returned from the function. 2:35 But a more common way to handle function returns is to assign them to a variable, 2:39 that looks like this. 2:44 So, in this example, I've created a variable called sum, and the value 2:53 that we're assigning to sum is the return value from the call to two_plus_two. 2:58 To break that down further, when the Python interpreter processes 3:04 the right-hand side of the statement, it calls the two_plus_two function, 3:07 which then calculates the value of 2 + 2 and returns it back. 3:11 The interpreter then takes that returned value and 3:16 assigns it to the new variable, sum. 3:18 Let's print out the value of sum to demonstrate this. 3:21 Now save and run. 3:29 This also prints out 4. 3:34 And as you can see, printing the function call is equivalent 3:36 to assigning the function call to a variable in printing that variable. 3:39 Now finally, we can use the function call in an expression. 3:44 Our function is returning an integer. 3:48 So let's try to take that value and multiply it by two. 3:50 How about you give this a try on your own? 3:53 Pause the video here and open up the attached workspace. 3:55 Add a return statement inside the two_plus_two function 3:59 that returns the val variable. 4:02 After the function, print out the product of the return value and the number 2. 4:05 When you're done, unpause the video to see the solution. 4:10 Okay, how did it go? 4:14 To use the function call in an expression, pretend it's a regular integer or 4:16 variable. 4:19 So, in the following example expression, 4 * 2, 4:21 we can replace the 4 with the function call. 4:25 Then we can print it out. 4:33 So we'll save and run. 4:40 Awesome, this prints out 8, just as expected. 4:45 That's because the return value from the function is, for and 4:48 we multiply that by 2 in our print statement. 4:51 Great work. 4:54 In the next video, we'll expand on our function discussion to show more about how 4:55 useful they can be and why we'd want to use them. 4:58 See you soon and keep on coding. 5:01
You need to sign up for Treehouse in order to download course files.Sign up