[MUSIC] Okay we've pointed out that local variables are visible only within the function in which they are defined. So even though the variable V was just given a value by sum all elements when we call it. Of course it was called myRand, and we called myRand and it called sum all elements, and then v was given a value. Even though all that happened we can't access v from inside the command window. Let's try it, v, told ya. In computer science terminology we expressed this local variable restriction by saying that a function's variables have local scope. Scope is defined as the set of statements that can access a variable. And this limitation that the set of statements that can access a variable are just inside the function of which the variable is defined it's actually a good thing. It keeps things simple, and local variables are all we need in the vast majority of cases. In this limitation of scope, to just the set of statements inside a function has a computer science term too. It's called local scope, let's look at that. Local scope is accessibility by statements in only one function or only in the command window but in some very, very rare cases, two or more functions and or the command window may need to share a variable. So maybe the command window needs to share this variable v now let's see how to do that. We do it by creating a variable whose scope can extend outside the function. Such a variable is called a global variable. Let's suppose you wanted to make v global. Then somewhere in the function before v is used, we insert this declaration, and I'm gonna put it right here before v is used. Global v, there. I'll put a semi-colon. You don't have to have one. By the way, in MATLAB, you cannot combine this global declaration with an assignment. So for example, you couldn't just do this. Global v and then put equals m colon, like that. And in that case you wouldn't need this statement at all. Do it like that. That would be handy. I mentioned that you can't do that because you can do it in some other languages. Okay, so let's go back. If I go right up here you'll see this little reverse under thing. Let's undo, undo, undo. There, we're back to where I wanted to be. Okay, now if you make the same declaration in the command window or in another function or in multiple functions or wherever you want to access this global variable. It'll be accessible there too. We'll do it in the command window. There, you can see v shows up here. Now when we run myRand. And let's do that. We'll just run myRand and give it, I don't know five, six. Makes no difference what we give it for this purpose. If we access V or attempt to from the command window, there it is. This time we can get V. Now what is V? It looks like a big long vector, and it is. If I scroll up, that's what it is. Remember, when we made v equal to m colon, this two dimensional array m had its columns stacked up into one long column vector and that's what we see here. But the point right now is that v is accessible both in some all elements and in the command window. Okay, now you know how to make global variables. But the next thing I'm gonna tell you is very important. Don't use them. Global variables can cause errors that are very difficult to diagnose because one function can change the values inside another function, like a ghost moving the furniture around, and you can't know which function did it. It's not that globals are inherently bad. They're not toxic and sometimes they are the best approach. Until you have some real experience with programming, you're far better off leaving them alone. You should probably avoid using global variable until you've been programming for at least, let's see, I'm gonna make it two years. For the rest of this course we won't need them at all and we certainly didn't need one here, so I'm gonna remove this one before I forget. [MUSIC] There, all gone. [MUSIC] [APPLAUSE]