1 00:00:00,430 --> 00:00:04,480 A closure is a function whose return value depends upon a variable 2 00:00:04,480 --> 00:00:07,220 declared out the function scope. 3 00:00:07,220 --> 00:00:10,200 Basically with closures, means a variable and 4 00:00:10,200 --> 00:00:13,410 a function where the variable is no longer in scope. 5 00:00:13,410 --> 00:00:15,180 Let's dive in and create a closure. 6 00:00:16,540 --> 00:00:20,460 Let's create a function called a reverse it which will take a string argument 7 00:00:20,460 --> 00:00:23,820 consisting of our superhero name and a prefix. 8 00:00:23,820 --> 00:00:27,240 Then it will concatenate the two and reverse the result. 9 00:00:27,240 --> 00:00:31,453 Let's dive back into the code editor and clean up some of our earlier code. 10 00:00:56,948 --> 00:01:01,660 So far, we've created a prefix variable which is not a vowel because we'll need to 11 00:01:01,660 --> 00:01:05,030 change this later in the reverse hit function. 12 00:01:05,030 --> 00:01:09,717 Let's see how we can use these two, to really show how powerful closures can be. 13 00:01:17,920 --> 00:01:22,790 In this case, we call the function reverse it by passing the value Thor. 14 00:01:22,790 --> 00:01:25,900 Let's also check out another example. 15 00:01:25,900 --> 00:01:28,349 Let's change prefix to "Super Fast." 16 00:01:41,040 --> 00:01:45,865 In the second example, we call reverseIt, passing it "Wonder Woman." And 17 00:01:45,865 --> 00:01:49,130 also we've changed the prefix to super fast. 18 00:01:49,130 --> 00:01:53,265 But what happens when we print out Thor and or when we print out Wonder Woman? 19 00:01:59,720 --> 00:02:02,080 Let's compile and check out the results. 20 00:02:04,540 --> 00:02:06,740 Although it's a bit difficult to read, 21 00:02:06,740 --> 00:02:10,050 we notice that that first prefix is set to lightning. 22 00:02:10,050 --> 00:02:15,140 And the reverse it function uses the variable prefix which is not in scope. 23 00:02:15,140 --> 00:02:18,320 We update the variable prefix to super fast. 24 00:02:18,320 --> 00:02:23,280 However, calling Thor retains the correct instance of prefix. 25 00:02:23,280 --> 00:02:26,130 I hope you enjoyed exploring Scala with me. 26 00:02:26,130 --> 00:02:29,130 We've gone through quite a few concepts through out the stages. 27 00:02:29,130 --> 00:02:31,580 And learned quite a bit about Scala. 28 00:02:31,580 --> 00:02:35,490 Checkout the teachers notes for more resources and how to proceed from here.