1 00:00:00,000 --> 00:00:01,400 [no audio] 2 00:00:01,400 --> 00:00:02,700 Friends, here we are going to 3 00:00:02,700 --> 00:00:05,600 discuss about scope of the variables for 4 00:00:05,600 --> 00:00:06,600 your functions. 5 00:00:07,500 --> 00:00:10,200 So before starting with the scope of the variables, let me 6 00:00:10,200 --> 00:00:13,700 show you how to call a function from another function. 7 00:00:14,900 --> 00:00:18,100 So I have taken a script name as 'scope_of_the_variables'. 8 00:00:20,100 --> 00:00:21,200 Right. Fine. 9 00:00:21,300 --> 00:00:24,000 Now, what I am doing is I'm going to define a function. 10 00:00:24,100 --> 00:00:26,400 Let's say simply I'm calling 11 00:00:27,700 --> 00:00:32,500 'myfunction1'. You can take any function names based on 12 00:00:32,500 --> 00:00:33,500 your rules. 13 00:00:34,300 --> 00:00:36,000 Let's say I want to display 14 00:00:36,000 --> 00:00:40,300 [no audio] 15 00:00:40,300 --> 00:00:42,100 "Welcome to functions". 16 00:00:43,600 --> 00:00:48,500 Right. So at the end just try to write 'return None'. That's it. 17 00:00:49,300 --> 00:00:52,600 Now, I'm going to define one more function, 'myfunction2'. 18 00:00:52,600 --> 00:00:59,200 [no audio] 19 00:00:59,200 --> 00:01:00,200 "Thank you". 20 00:01:01,100 --> 00:01:05,200 Then just write 'return None'. That's it. 21 00:01:05,800 --> 00:01:08,700 Now you have two functions in your code. 22 00:01:09,700 --> 00:01:13,100 So to execute, to get the output from your functions, what 23 00:01:13,100 --> 00:01:17,100 we have to do is, we need to call them, right. Suppose 24 00:01:17,100 --> 00:01:19,300 I am going to call 'myfunction1'. 25 00:01:20,600 --> 00:01:24,000 So this is the way how we can call your functions, right. See 26 00:01:24,000 --> 00:01:28,700 that. You're getting your output from first function. 27 00:01:29,400 --> 00:01:33,200 Now, if you want to execute your code which is there in second 28 00:01:33,200 --> 00:01:34,600 function as well, what we have to do 29 00:01:34,600 --> 00:01:37,200 is, we can call your second function. 30 00:01:37,200 --> 00:01:39,200 [no audio] 31 00:01:39,200 --> 00:01:40,200 That's it. 32 00:01:40,700 --> 00:01:43,600 But sometimes while going forward while writing real-time 33 00:01:43,600 --> 00:01:47,500 scripts, you may need to call your function from inside of 34 00:01:47,500 --> 00:01:48,500 another function. 35 00:01:49,000 --> 00:01:50,500 Yes, you can call it. 36 00:01:50,700 --> 00:01:54,600 Let's say I want to call 'myfunction2' from 'myfunction1', 37 00:01:54,600 --> 00:01:57,900 so I am calling from my function. 38 00:01:57,900 --> 00:02:00,100 [no audio] 39 00:02:00,100 --> 00:02:02,100 That's it. You're getting your output. 40 00:02:02,100 --> 00:02:06,100 "Thank you". Right. So from function one, I'm calling function 41 00:02:06,100 --> 00:02:09,900 two. Yes, you can call any function from any place. 42 00:02:11,600 --> 00:02:13,100 Right. Fine. 43 00:02:14,500 --> 00:02:18,500 Now, what I am doing is, and one more thing guys, if you observe, 44 00:02:18,500 --> 00:02:23,000 now this function is in your 'main' code, right? 45 00:02:23,400 --> 00:02:26,400 If you observe it is outside of your function. 46 00:02:26,400 --> 00:02:28,600 [no audio] 47 00:02:28,600 --> 00:02:32,400 But while going forward in your real time, right, maximum 48 00:02:32,400 --> 00:02:36,800 cases we won't write any logic outside of a function each and 49 00:02:36,800 --> 00:02:40,000 everything even suppose sometimes if you have a single line 50 00:02:40,200 --> 00:02:44,300 we'll try to write that line in terms of a function. 51 00:02:44,300 --> 00:02:46,700 That's a good practice, right? 52 00:02:47,700 --> 00:02:48,700 That's fine. 53 00:02:48,700 --> 00:02:50,600 [no audio] 54 00:02:50,600 --> 00:02:54,300 Now see that, what I am doing is, I am writing some 'x=10', 55 00:02:54,900 --> 00:02:58,700 and I want to print that 'x' value from your function 56 00:02:58,700 --> 00:03:06,800 one. Let's say, "x value from fun1 :", just I'm trying to 57 00:03:06,800 --> 00:03:09,000 print 'x' value. At the same time 58 00:03:09,100 --> 00:03:10,800 let me print the 'x' value, 59 00:03:10,800 --> 00:03:12,700 [no audio] 60 00:03:12,700 --> 00:03:19,200 "x value from fun2: ". Yes. Now, let me run it and see the 61 00:03:19,200 --> 00:03:21,200 output. You are getting your output. 62 00:03:21,200 --> 00:03:23,000 [no audio] 63 00:03:23,000 --> 00:03:25,800 Right. Now I am commenting this. 64 00:03:25,800 --> 00:03:28,400 [no audio] 65 00:03:28,400 --> 00:03:33,400 And in function one, I am defining 'x = 10'. Not only 66 00:03:33,400 --> 00:03:34,700 10, you can take any value. 67 00:03:34,700 --> 00:03:38,500 Let's say, 60. Now see the result. 68 00:03:39,600 --> 00:03:42,900 Observe the output. From your function one you are getting 69 00:03:42,900 --> 00:03:47,500 value as 60, but while executing from function two you are 70 00:03:47,500 --> 00:03:50,200 getting an error called, name 'x' is not defined. 71 00:03:50,900 --> 00:03:56,600 So this is called scope of the variables, means if any variable 72 00:03:56,600 --> 00:04:01,700 is defined inside of a function, by default that variable 73 00:04:02,600 --> 00:04:06,800 is specific to that function only. You can't access that variable 74 00:04:07,000 --> 00:04:08,400 outside of that function. 75 00:04:09,100 --> 00:04:13,200 So I defined 'x = 60' here, and I'm trying to display 'x' 76 00:04:13,200 --> 00:04:14,900 value from your second function. 77 00:04:15,300 --> 00:04:16,300 It won't work. 78 00:04:17,300 --> 00:04:22,200 It is specific to this function, or this is a local variable 79 00:04:22,200 --> 00:04:23,300 for this function. 80 00:04:24,800 --> 00:04:28,100 But if you observe, previously we did this one, right. Let's 81 00:04:28,100 --> 00:04:29,900 say first, let me comment this. 82 00:04:29,900 --> 00:04:32,100 [no audio] 83 00:04:32,100 --> 00:04:33,500 Whenever if any variable is 84 00:04:33,500 --> 00:04:37,700 defined outside of a function that variable you are able 85 00:04:37,700 --> 00:04:41,600 to access from function 1 and function 2. That means if any 86 00:04:41,600 --> 00:04:45,800 variable is defined outside of a function, then that is 87 00:04:45,800 --> 00:04:46,800 global variable. 88 00:04:47,600 --> 00:04:51,200 So by default if you define any variable outside of a function, 89 00:04:51,300 --> 00:04:52,500 see nowhere we have here, 90 00:04:52,500 --> 00:04:54,300 we don't have any function here, right? 91 00:04:54,700 --> 00:04:58,800 We are calling, but 'x = 10' in the 'main' code, it is not 92 00:04:58,800 --> 00:05:03,900 under any function. Then this 'x' is by default global variable. 93 00:05:04,200 --> 00:05:07,800 Global variable means, you can access that variable anywhere 94 00:05:07,800 --> 00:05:10,600 inside of your function, inside of your script. 95 00:05:11,900 --> 00:05:14,500 Right. Now what we are doing. 96 00:05:15,500 --> 00:05:18,600 If any variable is defined inside of a function, then that 97 00:05:18,600 --> 00:05:21,400 variable is specific to your function only. 98 00:05:22,700 --> 00:05:27,400 Right, now we have two types of variables, local and global variables. 99 00:05:28,400 --> 00:05:31,900 Right. First thing, scope means this is the meaning for your scope 100 00:05:31,900 --> 00:05:32,900 of the variable. 101 00:05:33,500 --> 00:05:37,000 So based on this scope, we have local and global variables. 102 00:05:37,400 --> 00:05:39,700 Now I can say this is a global variable. 103 00:05:39,700 --> 00:05:47,300 [no audio] 104 00:05:47,300 --> 00:05:52,300 And now if I enable this. Let's say this is local. 105 00:05:52,300 --> 00:05:56,100 [no audio] 106 00:05:56,100 --> 00:05:58,200 Now I'm running. See the result what you are getting. 107 00:05:58,200 --> 00:06:00,200 [no audio] 108 00:06:00,200 --> 00:06:05,500 So in 'main' code I have taken 'x = 10', and in 'myfunction1' 109 00:06:05,500 --> 00:06:10,100 I am defining 'x = 60'. Now, whenever if we take same 110 00:06:10,100 --> 00:06:14,000 variables in the 'main' code, and in the function, they are 111 00:06:14,000 --> 00:06:17,400 not same. Now this 'x' is different, and this 'x' is different. 112 00:06:18,600 --> 00:06:22,200 That's why while executing from 'myfunction1', 113 00:06:22,200 --> 00:06:24,900 what is the result you are getting? 60 value, because 114 00:06:24,900 --> 00:06:26,100 you defined 60 here. 115 00:06:27,000 --> 00:06:31,000 But from function two we don't have any 'x' variable here, then 116 00:06:31,000 --> 00:06:32,700 what your Python will do means, 117 00:06:32,700 --> 00:06:36,400 is there any global variable related with 'x', then we'll try to 118 00:06:36,400 --> 00:06:40,800 print that 'x', but before that in case if we have a local variable, 119 00:06:42,300 --> 00:06:44,200 now see the result, you are getting 40. 120 00:06:45,300 --> 00:06:48,300 So your function will give, your Python will give priority 121 00:06:48,300 --> 00:06:50,700 for your local variables inside of your function 122 00:06:51,900 --> 00:06:55,800 while trying to display your 'x' variable value. If 'x' is not 123 00:06:55,800 --> 00:06:57,500 defined inside of your function, 124 00:06:57,600 --> 00:07:02,000 I mean, if no local variable 'x', then what your Python will 125 00:07:02,000 --> 00:07:06,400 do means, it will try to find is there any 'x' as a global variable? 126 00:07:06,800 --> 00:07:09,400 If it is there you will try to print that variable value. 127 00:07:10,400 --> 00:07:13,800 If it is not there, then it will throw an error. See that, now 128 00:07:13,800 --> 00:07:16,800 I am commenting 'x = 10' in your 'main' code. 129 00:07:18,100 --> 00:07:19,100 Now you're getting an error. 130 00:07:20,100 --> 00:07:23,700 That's it. So this is simply scope of the variables. 131 00:07:23,700 --> 00:07:25,700 [no audio] 132 00:07:25,700 --> 00:07:27,800 Right, and last one, 133 00:07:28,600 --> 00:07:32,900 let me explain that. Now while going forward, right, 134 00:07:34,000 --> 00:07:36,900 we are going to write each and everything in terms of functions 135 00:07:36,900 --> 00:07:39,100 only. Now what I will do is, 136 00:07:39,100 --> 00:07:44,800 [no audio] 137 00:07:44,800 --> 00:07:46,100 you can take any function, not 138 00:07:46,100 --> 00:07:50,600 only 'main', but if we take 'main' then somewhat meaningful that's 139 00:07:50,600 --> 00:07:51,700 it. 'main' means 140 00:07:51,700 --> 00:07:54,100 your code will start from this place. That's it. 141 00:07:54,400 --> 00:07:55,700 Other than that nothing is there. 142 00:07:55,700 --> 00:07:58,900 [no audio] 143 00:07:58,900 --> 00:08:01,700 Defining 'x = 10', and calling 'myfunction1', 144 00:08:01,800 --> 00:08:05,900 I'm also taking as under one function, right. Now, 145 00:08:05,900 --> 00:08:09,300 if I run this there is no output because you defined three 146 00:08:09,300 --> 00:08:13,600 functions, and nowhere you are calling the execution point 147 00:08:14,300 --> 00:08:17,200 from where you want to start your code, whether you want to 148 00:08:17,200 --> 00:08:21,200 start from function 2, or function 1, or from main function, 149 00:08:21,300 --> 00:08:25,200 from which place you want to execute your code. As of now, 150 00:08:25,200 --> 00:08:28,800 right, I am feeling that this is my 'main' code. I have to start 151 00:08:28,800 --> 00:08:31,800 execution from this point, that's why I need to call this 152 00:08:31,800 --> 00:08:32,700 function somewhere. 153 00:08:33,200 --> 00:08:34,700 So now I am calling 'main'. 154 00:08:34,700 --> 00:08:37,400 [no audio] 155 00:08:37,400 --> 00:08:38,400 That's it. 156 00:08:39,299 --> 00:08:42,100 Now, whenever if you are trying to call in this way, you're 157 00:08:42,100 --> 00:08:45,500 getting an error called, name 'x' is not defined, from your 158 00:08:46,500 --> 00:08:50,000 'main', in which function? 'myfunction2'. 159 00:08:50,000 --> 00:08:52,100 [no audio] 160 00:08:52,100 --> 00:08:58,700 Right. Now, what I want to do is, as of now if you observe 'x = 10' 161 00:08:58,700 --> 00:09:00,100 is not a global variable, right? 162 00:09:00,300 --> 00:09:03,300 This is under, let me delete this. 163 00:09:03,900 --> 00:09:06,500 Now, this is under the 'main' function. 'main' is also one function 164 00:09:06,500 --> 00:09:07,500 like 'myfunction'. 165 00:09:08,400 --> 00:09:12,800 So instead of 'main' you can also take 'mycodestartsfromhere'. 166 00:09:12,800 --> 00:09:16,300 [no audio] 167 00:09:16,300 --> 00:09:19,700 You can take anything, but just we are following. Now 168 00:09:19,700 --> 00:09:22,700 we have to right here, instead of 'main' you have to write 169 00:09:22,700 --> 00:09:23,700 this one. That's it. 170 00:09:24,300 --> 00:09:25,800 Now see the result. Same thing. 171 00:09:25,800 --> 00:09:28,600 [no audio] 172 00:09:28,600 --> 00:09:32,600 So instead of this, just generally all are following like 173 00:09:32,600 --> 00:09:39,000 this. 'main', that's it. Now, instead of this, 'main'. Right. 'main' is 174 00:09:39,000 --> 00:09:40,700 also like one function. 175 00:09:42,000 --> 00:09:45,500 Now if you observe inside of your 'main', 'x = 10' is there. 176 00:09:45,900 --> 00:09:49,300 Now, this is a function, right. Now 'x = 10' is local, local 177 00:09:49,300 --> 00:09:50,500 for this 'main' function. 178 00:09:51,400 --> 00:09:55,800 You can't access now 'x = 10' from outside of your function, 179 00:09:55,800 --> 00:09:58,600 'main' function, or any of your functions. 180 00:09:59,600 --> 00:10:00,800 I defined 'x = 60', 181 00:10:00,800 --> 00:10:02,700 that's why from my 'myfunction1'. 182 00:10:02,700 --> 00:10:06,400 I can able to access 'x = 60', but in 'myfunction2' I 183 00:10:06,400 --> 00:10:08,700 don't have 'x' value. Then how can I get it? 184 00:10:08,700 --> 00:10:12,000 That's why you're getting an error. Now whatever the variable 185 00:10:12,000 --> 00:10:15,100 is there in 'main' function, 'x = 10', that variable 186 00:10:15,100 --> 00:10:21,700 value I want to access from 'myfunction2'. Then how? In case 187 00:10:21,700 --> 00:10:24,100 if it is there here, then that is the 'main' function, 188 00:10:24,100 --> 00:10:27,000 I mean in main logic you have, now you're getting your 'x' value 189 00:10:27,000 --> 00:10:32,800 from function 2 as 10, but now 'x = 10' is there in 'main' 190 00:10:32,800 --> 00:10:36,000 function, but I want to access this 'x = 10' from 'myfunction2'. 191 00:10:36,000 --> 00:10:37,800 [no audio] 192 00:10:37,800 --> 00:10:39,700 Right. Then in 'main' function, 193 00:10:40,200 --> 00:10:43,400 I mean whatever the function you have, in that function you 194 00:10:43,400 --> 00:10:44,800 have to write 'global x'. 195 00:10:45,400 --> 00:10:47,100 That means now 'x' is global. 196 00:10:47,100 --> 00:10:50,700 You can access my 'x' variable anywhere inside of your Python 197 00:10:50,700 --> 00:10:55,100 Script. Now see the result. Now you're getting from function 2, 10. 198 00:10:55,100 --> 00:11:00,100 [no audio] 199 00:11:00,100 --> 00:11:06,300 Fine. But guys making your variable as global is not always good. 200 00:11:07,700 --> 00:11:12,500 Okay. Sometimes you can take it as a, variable as global, 201 00:11:13,200 --> 00:11:18,000 but instead of global we have one more way to pass your 202 00:11:18,000 --> 00:11:21,200 corresponding variable to your required function. 203 00:11:22,400 --> 00:11:25,700 Right. See that. Now, I am going to comment from here. 204 00:11:25,700 --> 00:11:28,000 [no audio] 205 00:11:28,000 --> 00:11:29,000 Don't get confused, 206 00:11:29,600 --> 00:11:32,100 okay. We'll discuss this again in next video. As of now just 207 00:11:32,100 --> 00:11:35,500 I am giving the way how you can pass. See now 208 00:11:35,500 --> 00:11:39,000 I'm going to comment this, and as of now if I run my code 209 00:11:39,000 --> 00:11:39,800 what you are getting? 210 00:11:40,900 --> 00:11:44,200 From 'myfunction1' you are getting your value as 60. Now, 211 00:11:44,200 --> 00:11:47,400 I'm not calling anywhere 'myfunction2'. Now, I want to call 212 00:11:47,400 --> 00:11:51,600 'myfunction2', suppose from 'main' function itself. 213 00:11:53,600 --> 00:11:57,400 Now I am calling, but in 'myfunction2' I don't 214 00:11:57,400 --> 00:11:58,600 have any variable called 'x', 215 00:11:58,600 --> 00:11:59,700 that's why we are getting an error. 216 00:12:00,800 --> 00:12:03,500 I don't want to use global. In case if I use global, 217 00:12:03,500 --> 00:12:07,500 yes, it will work without an error, but I don't want to use 218 00:12:07,500 --> 00:12:08,800 variable as a global. 219 00:12:10,100 --> 00:12:15,400 But I want to send the value of, 'x' value 10 to 'myfunction2', 220 00:12:16,000 --> 00:12:20,000 so other way to pass your value to your required function 221 00:12:20,000 --> 00:12:21,900 is just inside of your parentheses, 222 00:12:21,900 --> 00:12:22,900 just write 'x'. 223 00:12:22,900 --> 00:12:25,100 [no audio] 224 00:12:25,100 --> 00:12:26,800 Now here also you have to write 225 00:12:28,400 --> 00:12:32,800 'x'. Now what will happen? The 10 value, while calling this 226 00:12:32,800 --> 00:12:38,000 'myfunction2' you are also passing, not 'x', you are not passing 227 00:12:38,000 --> 00:12:42,600 'x'. The value of 'x', 10, 10 you are passing. 228 00:12:43,200 --> 00:12:47,700 So the meaning for this is, you are not passing 'x', value of 229 00:12:47,700 --> 00:12:53,500 'x', 10 you're passing to this variable. Now this 'x' and this 230 00:12:53,500 --> 00:12:56,900 'x', they are completely different. In case if you are 231 00:12:56,900 --> 00:12:58,100 having a confusion, 232 00:12:58,200 --> 00:13:01,400 right, first let me run it and see the result. 233 00:13:01,400 --> 00:13:05,500 Now what I am doing, instead of 'x', I'm taking 'y'. Now, 234 00:13:05,500 --> 00:13:06,900 I want to print that 'y'. 235 00:13:07,900 --> 00:13:08,900 That's it. 236 00:13:09,800 --> 00:13:16,000 Right. So other than global, how can you, if I take global 237 00:13:16,000 --> 00:13:18,100 then this variable is, 238 00:13:19,900 --> 00:13:23,100 you can access that variable from any function, from any line 239 00:13:23,100 --> 00:13:26,400 anywhere in your Python Script. But I don't want to access 240 00:13:26,500 --> 00:13:29,600 in all places, only from this function only. 241 00:13:29,600 --> 00:13:33,200 I want to access this variable 'x', that value, not even that 242 00:13:33,200 --> 00:13:37,100 'x', only the value of 'x', then I can send in this way. 243 00:13:38,300 --> 00:13:43,000 That's it. And one more important point is, now this variable and this 244 00:13:43,000 --> 00:13:46,900 variable are completely different. The value of 'x' will be assigned 245 00:13:46,900 --> 00:13:49,100 to 'y', that value we are printing. 246 00:13:49,400 --> 00:13:51,400 That's it. Okay. 247 00:13:53,100 --> 00:13:57,300 So now here, whenever if you pass your variable to your function, 248 00:13:57,300 --> 00:13:59,500 then that is called argument. 249 00:14:01,000 --> 00:14:03,900 And here that is called 250 00:14:03,900 --> 00:14:06,100 [no audio] 251 00:14:06,100 --> 00:14:11,200 parameter. Okay. We will discuss in detail in the next video 252 00:14:11,200 --> 00:14:14,000 about this argument and parameter, okay. 253 00:14:14,800 --> 00:14:16,800 Okay guys, thank you for watching this video. 254 00:14:16,800 --> 00:14:24,602 [no audio]