[MUSIC] Let's review what happened
when the function ran. It was only one statement,
so it's pretty simple. The expression over here on
the right was evaluated, it was assigned to the variable, “a”, and since we didn't put a semicolon
at the end of the statement . . . By the way, that's why this
little orange highlight shows up, and there's a little hint there that says, “Terminate statement with semicolon
to suppress output (in functions).” That only happens——that little
yellow thing only happens in——a function. Anyway, we didn't put a semicolon there. so MATLAB printed the value
of “a” in the command window. No surprise there. Let's print that value again. It's undefined! If you check the workspace,
it's not there. Why? We didn't clear it. So where did it go? Has this Workspace quit working? Something strange is happening here. Let's investigate. Let's assign a simple scalar
value to “a”, say, three. And there it is in the workspace,
as it ought to be. “a” equals 3. Okay, let's run myRand again. We got a new set of random numbers as
expected, but look over here at “a”. Can its value still be three? Wait a minute. Yep, its value is still three. It's not a random matrix, what's going on? Well, mon ami, allow me to elucidate. There's a saying in Nevada:
What happens in Vegas, stays in Vegas. And that saying fits what's happened here. A function has its own private workspace
and what happens in that workspace, stays in that workspace. During that little fraction of
a second that myRand was running . . . I should say, fraction of a microsecond . . . the variable “a” was created right here, stored in myRand's workspace, and
then, when myRand was done, its workspace was deleted. It doesn't show up in
the command window's workspace. The “a” that we see there
is a completely different variable from the variable
that was created over here. The one that was assigned this
array of random numbers is gone forever along with
these values down here. All we're left with is
the printout of those values. You can look at them, admire them, but
we can't assign them to a variable in the command window or
use them in any other way. The variables in the disappearing
workspace are called “local variables” in computer science terminology. These are variables that are defined
by assignment statements inside the function, and
they can be used only inside the function. Here's the textbook definition: “Local variable. a variable that is accessible only
by statements inside one function. A local variable exists only
during the function call.” Well, this is not good. A function typically calculates a result, and
if we can't use the result of the calculation after the function
closes, what good is it? We need to be able to
propagate that result from inside the function out
into the outside world. We do that with an output argument. Let's see how that works. Let's change myRand just a little bit
here to make “a” an output argument. We do that by typing “a equals” right
here between the “function” keyword and the name of the function. Now let's save our function. We come up here and you'll notice that
this little diskette is blueish and I'm going to click on it and
watch it change to grey. There. That means what's in the file here
is the same as what's on the screen. And that's important,
because when we call the function, the code that will run is
the code that's inside the file. Now let's run myRand. [CLICKING] Now we get the result twice here and here. So why is that? Well first, while the function's running, MATLAB prints the result of this
assignment statement right up here, because we don't have
a semicolon at the end of it. So this is the first printout and
you can see it says, “a equals”, because we have “a equals” up here. And then after the function is done,
down here in the command window, MATLAB echoes the result of the value
that's returned from this call of myRand right here, and here it is because
we don't have semicolon after that. These two values are the same, so we see
the same numbers twice here and here. Oh, and in case you've forgotten this
variable “ans” that appeared out of nowhere is that variable that's automatically
created by MATLAB whenever it calculates a value that's not explicitly
assigned to a variable. This wasn't explicitly
assigned to a variable and so, it made this variable
ends to accept the value. And what about “a”? Looks like it's finally gotten
the values we wanted it to have, so let's double check that. No! It's still equal to three
because this “a” here, the one we see in this workspace, is
the one that we set in the command window, not the one that we
set up here in the function. It makes no difference that we declared
“a” here to be an output argument. An output argument is
still a local variable, and it lives only as long as
the function is running. It just happens to be the local
variable that holds the value that we wanted the function to output. It still disappears as soon
as the function's done, just a split section after its value
is sent to the command window. No matter how many times we run myRand, the “a” in the command
window will be unaffected. It's not the “a” in the function,
so it just stays equal to three. Okay.
Let's tell MATLAB not to print from inside the function. We don't need that. Semicolon right here
should take care of that. Now when we run myRand, this statement
inside the function right here won't cause any printout and we'll see
only one copy of the random numbers. But before I run it, I want you to
note that the diskette icon is blue over here, indicating that we've
changed the contents of the editor, but we've not yet saved those contents
into the m-file named myRand. And there are two other indications
that we've not saved yet. The file name here in
the current window and the file name here on this
little tab in the Editor both have an asterisk after the name. That also means that we haven't saved. Now, I'm going to click in the command
window and watch what happens. There. This disk icon is grey, this file name doesn't have
an asterisk after it, and this file name doesn't
have an asterisk after it. This is a new feature of
the latest version of MATLAB. When you click anywhere outside
the Editor window in version 2014 B. MATLAB automatically saves
the function into the file. If you have an earlier version, you'll
need to remember to click the diskette icon, or use the appropriate keyboard
shortcut, such as Ctrl+S or Cmd+S, or whatever your operating system might
provide to save the file before you run it in the command window. Okay, let's run it again. Now, only one copy of the output appears. That's good, but we should still capture
the output into a variable of our choosing instead of “and”, you know,
which is the variable that MATLAB chooses. So, how about . . .
uh, I don't know . . . b. There, we look over here,
we see that there's a b, and it says it's a three
by four array . . . “double” We'll explain what that means later. You know, you can look at these variables,
so let's click that, double click it, and you see the array
laid out here, that's inside b. And I'm going to close that. Of course, we don't usually
need to see this output, so one last thing, we could put a semicolon. This time I'm going to use a c . . . semicolon. So, now nothing happens here. But the c gets the value, as you can see. Here it is. There, c has the value. There's the c. c got a different value than b
because every time we run myRand we get a different set of random numbers. Okay, we have our function
working perfectly. What if we also wanted a three
by four random matrix with values between two and five,
as opposed to one and ten? What would the formula look like for that? Let's see. Right now, we got the numbers
ranging between one and ten. And we did that by adding one here and multiplying by nine. And nine was the range between values
from one to ten, which is ten minus one. So, we could get the numbers
that range from two to five this way. [CLICKING] And those numbers look like they range from two to five. The reason I wrote 5 minus 2 instead
of 3 will be apparent in a minute. Okay, so to use this formula
should we write another function? No. What we should do is modify
myRand to be more general. It should be able to provide a three by
four matrix of random numbers between any two limits we want. We can do this by providing two input
arguments to the function that specify the limits we want, then using
those two arguments in the formula. Let's do that. The input arguments come after the
function name here, and go in parentheses. And if there are two or more of them,
they must be separated by commas. Let's name them low and high. Before we go further, I want to take
a minute to explain this yellow pop-up. This thing right here. It's a tip to help us with programming. It says that the input argument low,
right here, might be unused, and it suggests what we might want to do,
if this is okay. Well, it's not okay. We are going to use it.
We just haven't used it yet. This tip is MATLAB's way of helping
us while we write our code. It's gently suggesting that we might
be forgetting to do something, because after all, why would we require an input to
myRand if we weren't going to use it? If we hover the mouse over high, we get the same sort of
warning about that argument. And also, both high and
low are highlighted, as you can see here to alert us that something might be wrong. These highlights will disappear, and
you'll notice that, when we use low and high in the calculation of “a”. And we're going to put both low and
high in that calculation right now. So here's low, and this, remember, is the difference: high minus low. By the way, input arguments,
just like all the other variables, and the output arguments in
a function are local variables. So, you can't read them outside. Now, you can see why I used 5 minus
2 instead of 3 down here when I was doing this calculation
of the random numbers from two to five. It was to emphasize that, when
you give MATLAB the range two to five or low to high, in general, the only way to know what the range is
that you're going to multiply by here, that is the difference between high and
low, is to subtract them. Well, okay, let's try out this more
versatile version of our new function. I'm going to put the value
in a variable called test and give it two arguments, 2 and 5. And I'll put a semicolon there so we won't have anything printing
out in the command window. Before I hit return,
let's think what happens. First, the input argument, low,
right up here, will get the value 2 because
it's the first argument. And the input argument high will get
the second value, 5, because it's second. Then, this function will use low and high in this expression to
calculate the random numbers. Third, since “a” is declared right
here to be an output argument, its value will be returned
when the function closes. It will show up right down here. The way a value is returned
is no different from the way a value's returned from any function,
like the built-in functions. When we say “return”, we simply mean that
the value, this value, whatever it is inside “a”, is inserted right here at the
point where the function was called, which in this case is in the command window
on the right side of this equal sign. Okay, let's execute. There. Let's look at test. Now it looks right. And the numbers seem to be between two and
five. Now let's try to take a look at low,
which was our, one of our input arguments. Well, we get a message here that says,
“Undefined function or variable ‘low’. Did you mean: flow.” Well, that's something else that
we might have meant, but no. What we wanted to do is
just establish again, and it shouldn't be a surprise,
that you get no low in the command window. This low is in here in myRand, and it's
just a local variable, just like high is. Neither one of them are available
outside the function. Now, let's tell myRand to use
the range we started with. Remember that, one to ten? Let's put that in test2. I don't think I'll put
a semicolon this time, so we can just take a look right at it. Yeah, looks like that
goes from one to ten. These numbers are between one and ten. Works like a charm. Wonder if it works with negative numbers? Let's go from -2, say, to 6. Did you notice that
little hint we got there? It's giving us a hint
with our own function. It'll do this with a built in function. It'll do this with any
function that you create. It shows you that there's couple
of arguments, low and high, and there they are up there,
just the remind you. So where was I? I was going to do -2 to 6. [CLICKING] -2 to 6. Yeah, it looks like these
numbers range from minus 2 to 6. We've got a really
versatile function here. Okay. Let's clear the screen and clear the workspace. Our function has two scalars as input and
one matrix as output, but what if you want your function
to return two outputs? No worries. There's an easy way to do that. Let's say we want myRand to compute
the sum of all the elements of the generated random matrix and
return both the matrix and the sum. Now let's add a couple of statements to
calculate the sum of the elements in “a”. I put my cursor there, hit Return. These two statements will do it. You might be surprised by
the first one of these statements. Since “a” is a matrix,
when we put parentheses after it, you'd probably expect to see us put
two indices inside them, you know, separated by a comma to specify
an element on a given row and column. Instead, we've given just one colon here. There's a special way
of indexing in MATLAB. When you give just one index for an array, MATLAB treats it as if all its
columns were stacked up into one long column vector and that index picks
out one element in that vector. And when that index is a colon, it means
you want all the elements in that vector. And in here, in this statement,
when you give the built-in function, sum, an argument that's a vector,
it returns the sum of all the elements in that vector, and
since v has all the elements of “a”, we're going to get the sum
of all the elements of “a”, and that's what we want here. By the way, if we were to give sum
the matrix a directly instead of a vector, it would calculate a set of sums, one for each column of “a” and
it would return them in a row vector, but that's not what we want here. Okay.
So we've got the sum. Now we've got the matrix in “a”. Let's see how to return both “a” and the sum that's in s now
that we've calculated them. We’ll make a very small change
in the header of myRand, that is in this first line, like this. There. We now have two output arguments, “a” and s. We can have as many output arguments as
we want as long as put them in square brackets, like this, and it's recommended
that they be separated by commas. Okay,
let's run it. First, we have to save it. I can save it automatically,
simply by clicking in the command window since this is version R2014B of MATLAB. But I'll remind those of you who are using
earlier versions that you'll need to save your functions manually
every time you change them. And you can do that by clicking on this
little blue diskette icon over here. Okay, here goes. I'm going to give it (2,3) as arguments, and there's our matrix. But wait, who store the sum? Where did it go? Well, we didn't save it in a variable,
so MATLAB threw it out. It catches only the first result of
a function, and the first result in this case was the matrix,
which was stored in “a”. If we want to catch two results, then we need to provide two
variables to catch them, like this. I'm going to give variables x and ss. [CLICKING] I'm going to do (2,3) again. There. Looks like
x got the matrix and ss has got the sum. Now you see the syntax for catching more
than one output returned by a function? We need the square brackets again, just as we did up here in the function
arguments, output arguments. And this time,
the comma is totally optional. Well, we've seen what happens
when we call the function without enough output arguments. We just lose some of them. What happens if we call it with
too many output arguments? Say, three, in this case. Let's go, I don't know, -10 to 10. We found out that negatives work and
MATLAB complains. “Error using myRand. Too many output arguments.” [MUSIC] Well, sorry MATLAB,
we'll try not to do that in future. [MUSIC] [APPLAUSE]