1 00:00:00,000 --> 00:00:02,699 There is one important thing I want to talk to you 2 00:00:02,699 --> 00:00:06,089 about before we continue, and this is the scope of 3 00:00:06,119 --> 00:00:10,229 a variable. The scope refers to where a variable 4 00:00:10,259 --> 00:00:14,099 exists and where it is available. Depending on 5 00:00:14,099 --> 00:00:17,699 where you create a variable, it may or may not be 6 00:00:17,699 --> 00:00:21,089 available to other parts of the program. And to 7 00:00:21,089 --> 00:00:23,789 explain this, I'm going to start with an example. 8 00:00:24,119 --> 00:00:27,029 So we are going to continue with our code here, we 9 00:00:27,029 --> 00:00:32,098 have so, call to print_double_number(3). And 10 00:00:32,098 --> 00:00:36,213 what I'm going to do after, I'm going to do print(result). 11 00:00:36,213 --> 00:00:39,629 I'm going to print the result variable 12 00:00:39,659 --> 00:00:42,599 that we have created here. Okay, so as you can 13 00:00:42,599 --> 00:00:45,359 see, as a warning, it's already red, so let's 14 00:00:45,359 --> 00:00:51,059 see what it does. And we have an error here. name 15 00:00:51,089 --> 00:00:55,349 .result' is not defined. Why is that? We create a 16 00:00:55,349 --> 00:00:58,079 variable result, and then we can't use it, it's 17 00:00:58,079 --> 00:01:02,069 not defined. This is because of the scope. So from 18 00:01:02,069 --> 00:01:04,229 what you have seen before, if you create a 19 00:01:04,229 --> 00:01:07,229 variable, let's say create a variable a is equal 20 00:01:07,229 --> 00:01:11,309 to two, then I can use the variable a, after I've 21 00:01:11,309 --> 00:01:14,339 created it. So after line 11, I can use the 22 00:01:14,339 --> 00:01:18,899 variable a as many times as I want. But when you 23 00:01:18,899 --> 00:01:22,559 create a variable inside a function like here, 24 00:01:22,889 --> 00:01:26,369 well, this variable will not be available from 25 00:01:26,429 --> 00:01:29,579 outside of the function, as you can see here, with 26 00:01:29,579 --> 00:01:34,049 this error. The variable that you create outside of a 27 00:01:34,049 --> 00:01:37,169 function, for example, here, a is equal to two, 28 00:01:37,529 --> 00:01:42,299 this is a global variable in the global scope, 29 00:01:42,449 --> 00:01:45,599 okay. And the variable that you create inside the 30 00:01:45,599 --> 00:01:49,859 function is what we call a local variable, which 31 00:01:49,889 --> 00:01:54,059 only exists inside this local scope of the 32 00:01:54,059 --> 00:01:57,629 function. So knowing this can avoid you a lot of 33 00:01:57,659 --> 00:02:00,359 errors in the future, okay. When you create the 34 00:02:00,359 --> 00:02:03,029 global variable, you can use it everywhere after 35 00:02:03,029 --> 00:02:05,579 you create it. But when you create variables 36 00:02:05,609 --> 00:02:08,669 inside functions, well, the variable can only be 37 00:02:08,669 --> 00:02:11,939 used inside the function. And this is also what's 38 00:02:11,939 --> 00:02:14,309 happening with parameters, okay. When you have 39 00:02:14,339 --> 00:02:17,939 parameters, any parameter you have here, you will 40 00:02:17,939 --> 00:02:20,609 be able to use it inside the function, okay, but 41 00:02:20,609 --> 00:02:23,519 this basically, for every parameter, it's going to 42 00:02:23,519 --> 00:02:27,239 create a new local variable inside the function. 43 00:02:27,479 --> 00:02:30,539 So here if I say, Hello, you have two local 44 00:02:30,539 --> 00:02:34,329 variable user_name and user_age. For double_number, 45 00:02:34,329 --> 00:02:36,839 you have a local variable, named number. 46 00:02:37,049 --> 00:02:39,449 And for print_double_number, you also have the 47 00:02:39,449 --> 00:02:42,599 local variable named number. And as you can see 48 00:02:42,599 --> 00:02:46,079 here, because the scope of this, scope of this, and 49 00:02:46,079 --> 00:02:49,319 the scope of this are different, here this is not 50 00:02:49,319 --> 00:02:53,309 a problem to use number twice, okay. Because this 51 00:02:53,339 --> 00:02:56,459 is actually not the same as this, this number is a 52 00:02:56,459 --> 00:02:59,039 local variable inside this function. And this 53 00:02:59,039 --> 00:03:01,769 number is another local variables inside this 54 00:03:01,769 --> 00:03:04,589 function. So you can have two variables with the 55 00:03:04,589 --> 00:03:07,919 same name in different scopes, and they are going 56 00:03:07,919 --> 00:03:11,219 to be different variables. Alright, so very 57 00:03:11,219 --> 00:03:13,859 important thing to ask yourself when you create a 58 00:03:13,859 --> 00:03:17,429 new variable, where does these variables exist. 59 00:03:17,789 --> 00:03:21,029 And in the current scope, I'm writing code right 60 00:03:21,029 --> 00:03:24,239 now, can I access this variable? Okay. If you want 61 00:03:24,239 --> 00:03:27,869 to access the user_age, well, you can only access 62 00:03:27,869 --> 00:03:30,959 the user_age inside the function say_hello. So if 63 00:03:30,959 --> 00:03:34,319 you write code here, yes, I can access user_age. 64 00:03:34,589 --> 00:03:37,919 Here, no, I can't access user_age and outside of the 65 00:03:37,919 --> 00:03:41,549 function neither and very basically, but when you 66 00:03:41,549 --> 00:03:44,729 create a new function, you create the new local 67 00:03:44,729 --> 00:03:47,909 scope with new local variables. Alright, so 68 00:03:47,909 --> 00:03:50,639 knowing this will help you understand some erros 69 00:03:50,639 --> 00:03:53,549 when you get started. And later on, it will help 70 00:03:53,549 --> 00:03:56,608 you also to avoid those errors.