Thank you for coming back to the course. Today we are going to talk about very important programming concept, which is the global and local variables. If you don't have a clear understanding of these paths, our program may not behave as expected. A global variable is defined outside of a function. And the whole program can see an SS DC variable. for local variable, it is defined inside a function and the content of the variable is only accessible inside the function.
Now if we define a global variable cash equals to 2000 and then we define the function wallet inside the function we'll clean out the variable cache. We want to see if we caught it cache variable inside a function, what will happen? We find that the function is able to assess the value of the global variable cache. This is mainly because the cache is a global variable which can be accessible everywhere in the application. Now, if we define the function call it again but this time inside the function, we define another local variable cash equals to 1000 and print that out. This time the function will return 1000 instead of 2000.
How about if we clean out the cash variable now, it will give us 2000. What has happened? First we define the global variable cash equals to 2000 inside the function for that it will be able to assess this global variable. So the first part of our application is not a problem. Native, let me define an other function for that. We have a local variable cache, this local variable is going to override the global variable inside the function.
So that's why when we print out cash inside the function, we will see 1000 instead of 2000. However, because this local variable can only be accessed inside the function, so later when we clean our cache again, this cache is referring to the global variable. That's why we got 2000. Now what if we want to modify the global cache variable inside the function? What can we do? We can define the wallet function again.
And then we use the key word Google before the verbal cache after that, we assigned cache to be 1000 again. And then we pin our cache. And let's see what happened. Do we still have executing the wallet function with the 1000 and when we print out cash again, you can see that the global cash variable has been modified. And this is all about the global and local variables.