1 00:00:00,470 --> 00:00:01,730 Welcome back. 2 00:00:01,730 --> 00:00:04,320 Now that you now to define and call a function, 3 00:00:04,320 --> 00:00:08,570 we have to expand our discussion a little bit to talk about something called scope. 4 00:00:08,570 --> 00:00:12,070 Scope refers to the visibility of parts of our program. 5 00:00:12,070 --> 00:00:15,570 Not all parts of a program can be seen by other parts of our program. 6 00:00:15,570 --> 00:00:19,530 This is really important to remember when you're thinking about functions. 7 00:00:19,530 --> 00:00:24,050 The code you write inside of a function is only accessible inside that function. 8 00:00:24,050 --> 00:00:28,260 That means if you assign a variable inside a function, you can't use that variable 9 00:00:28,260 --> 00:00:32,700 in another part of your program and expect it to reference the same value. 10 00:00:32,700 --> 00:00:35,080 This can be a little hard to grasp without seeing it. 11 00:00:35,080 --> 00:00:36,090 Let's jump into some code. 12 00:00:37,230 --> 00:00:39,670 Okay, take a look at my workspace here. 13 00:00:39,670 --> 00:00:43,393 You see we have a variable assignment num=10. 14 00:00:43,393 --> 00:00:45,515 Then you see a function set_num. 15 00:00:47,360 --> 00:00:50,210 Inside the set_num function there's another variable assignment. 16 00:00:50,210 --> 00:00:55,885 Num=5 after the function definition the program calls set_num. 17 00:00:57,090 --> 00:01:00,020 What do you think will be printed when this program is run? 18 00:01:00,020 --> 00:01:01,610 Pause the video here. 19 00:01:01,610 --> 00:01:06,300 Open up the attached workspace, and run the file in the workspace terminal, 20 00:01:06,300 --> 00:01:09,416 see what's printed, and then come join me again. 21 00:01:09,416 --> 00:01:10,755 All right, how did it go? 22 00:01:10,755 --> 00:01:13,950 Were you a little surprised by the value that was printed? 23 00:01:13,950 --> 00:01:14,590 Let's break this down. 24 00:01:15,650 --> 00:01:19,220 The variable assignment num = 10 takes place inside 25 00:01:19,220 --> 00:01:23,050 what is known as the global context, or the global scope. 26 00:01:23,050 --> 00:01:26,300 It's global because it's not inside any other function, 27 00:01:26,300 --> 00:01:28,720 it's at the highest level in the program. 28 00:01:28,720 --> 00:01:32,100 Any other part of this program can see, access, and 29 00:01:32,100 --> 00:01:35,690 use this variable including the code inside the set_num function. 30 00:01:36,710 --> 00:01:38,920 So when the set_num function is called and 31 00:01:38,920 --> 00:01:44,510 Python encounters the num = 5 variable assignment, it doesn't reassign num, it 32 00:01:44,510 --> 00:01:49,400 creates a new num that only exist inside the local context of the set_num function. 33 00:01:50,460 --> 00:01:54,750 That means that outside of set_num, even if we've already called it, num will 34 00:01:54,750 --> 00:01:59,700 always reference 10 until this reference has changed in the global context. 35 00:01:59,700 --> 00:02:03,220 Inside the set_num function however, num will have a value of five. 36 00:02:05,410 --> 00:02:08,790 Additionally, if we create a variable inside set_num, 37 00:02:08,790 --> 00:02:11,580 we can't access it outside of that function. 38 00:02:11,580 --> 00:02:14,658 I'll create a new variable inside the function called letter, and 39 00:02:14,658 --> 00:02:16,124 it'll reference the letter a. 40 00:02:21,050 --> 00:02:24,596 Let's see what happens when I try to print the letter variable outside of set_num. 41 00:02:28,810 --> 00:02:31,220 I'm gonna save, run this down in my terminal. 42 00:02:37,601 --> 00:02:39,490 Okay, we're getting an error here. 43 00:02:39,490 --> 00:02:44,000 A name error that says name 'letter' is not defined. 44 00:02:44,000 --> 00:02:48,970 Well, that's because there is no letter variable inside the global scope. 45 00:02:48,970 --> 00:02:52,060 Letter only exists inside the set_num function and 46 00:02:52,060 --> 00:02:54,860 can only be accessed inside of it. 47 00:02:54,860 --> 00:02:57,812 If we move our print statement inside the function and try again, 48 00:02:57,812 --> 00:02:59,700 we'll probably get a different result. 49 00:03:03,825 --> 00:03:05,430 Now I'm gonna save and try it again. 50 00:03:08,248 --> 00:03:09,780 Cool, it printed the letter a. 51 00:03:10,870 --> 00:03:13,660 So now that you know a little bit about global and local scope, 52 00:03:13,660 --> 00:03:18,020 you might be wondering how you actually get values and variables out of functions. 53 00:03:18,020 --> 00:03:21,410 A variable's created inside a function, in the local scope, 54 00:03:21,410 --> 00:03:24,950 can't be seen in the global scope, then what's the point. 55 00:03:24,950 --> 00:03:25,900 Stay tuned to find out.