[MUSIC]
Okay. We've pointed out that local variables
are visible only within the function in which they're defined.
So, even though the variable v was just given a value by sumAllElements when
we called it.
Of course, it was called from myRand.
We called myRand, and it called sumAllElements, 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 you! In computer science terminology, we express this local
variable restriction by saying that a function's variables have local "scope".
[CLICKING]
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
is actually a good thing.
It keeps things simple, and
local variables are all we need in the vast majority of cases.
And 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.
Well, 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 going to put it right here before v is used
[CLICKING]
global v
There.
I'll put a semicolon. 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'll 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 undo thing.
Let's 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.
[CLICKING]
There, you can
see v shows up here.
Now when we run myRand, and let's do that.
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 sumAllElements and in the Command Window.
Okay. Now you know how to make global variables.
But the next thing, I'm going to 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,
you know, 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're the best approach.
But until you have some real experience with programming
you're far better off leaving them alone.
You should probably avoid using global variables until you've been
programming at least ... Let's see ... I'm going to 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 going to remove this one before I forget.
[MUSIC]
There.
All gone.
[MUSIC]
[APPLAUSE]