[MUSIC] Let's review what happened in the function rand. 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 the semicolon at the end of statement, by the way, that's why this little orange high light shows up. There's a little hint there that says terminate statement with semicolon to suppress output. In functions, that little yellow thing only happens in 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 3. There it is in the workspace as it ought to be, a equals 3. Okay, let's run myRand again. We've got a new set of random numbers as expected. But look over here at a. Can its value still be 3? Wait a minute. Yep. a's value's still 3. 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 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. We 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 the 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 = 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 bluish, and I'm gonna click on it, and watch it change to gray. 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 is inside the file. Now let's run myRand. Now we get the result twice, here and here. So why is that? Well first, while the functions running, MATLAB prints the result of this assignment statement right up here, because we don't have a semicolon here at the end of it. So this is the first printout, and you can see it says a = because we have a = up here. And then after the function is done, down here in the command window, MATLAB echos the result of the value that's returned from this call of myRand, right here, and here it is, because we don't have a 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 ans to accept the value. And what about a? It looks like it's finally gotten the values we wanted to have, so let's double-check that. No. It's still equal to 3, 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 second 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 3. Okay, let's tell MATLAB not to print from inside the function. We don't need that. A 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 gonna click in the command window, and watch what happens. There. This disk icon is grey, this filename doesn't have an asterisk after it, and this filename 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 2014b, 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 to 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 ans, which is the variable that MATLAB chooses. How about, I don't know, b. There. We look over here. We see that there's a b. And it says it's a 3 by 4 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 gonna close that. Of course, we don't usually need to see this output, so one last thing. We could put a semicolon, and this time I'm gonna 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 3 by 4 random matrix with values between 2 and 5 as opposed to 1 and 10? What would the formula look like for that? Let's see, right now, we got the numbers ranging between 1 and 10 and we did that by adding 1 here and multiplying by 9. 9 was the range between the values from 1 to 10 which is 10 minus 1. So, we could get the numbers that range from 2 to 5 this way. And those numbers look like they range from 2 to 5. The reason I wrote 5- 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 3 by 4 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 wanna 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 wanna do if this is okay. Well, it's not okay. We are gonna 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 aren't gonna 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 gonna put both low and high into 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- 2 instead of 3 down here when I was doing this calculation of the random numbers from 2 to 5. It was to emphasize that, when you give MATLAB the range 2 to 5, or low to high, in general, the only way to know what the range is that you're gonna 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 gonna put the value in a variable called test and give it two arguments, 2 and 5, and I'm gonna put a semicolon there so it 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 is 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 equals side. Okay, let's execute. There. Let's look at test. Now, that looks right. The numbers seem to be between 2 and 5. Now let's try to take a look at low, which was one of our input arguments. Well, we got 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 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 1 to 10, 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 1 to 10. These numbers are between 1 and 10. 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 a couple of arguments, low and high, and there they are up there, just to remind you. So where was I? I was going to do -2 to 6. -2 to 6. Yeah, looks like these numbers range from -2 to 6. We've got a really versatile function here. Okay let's clear the screen and clear the workspace. The function has two scalars has input and one matrix as output. 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. Well, let's add a couple of statements to calculate the sum of the elements in a. Put my cursor there and 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 would probably expect us to put to indices inside them, separated by a comma to specify an element on a given row and column. Instead we've given just one colon here. This is 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 under 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 all the elements in that vector, and since v has all the elements of a, we're gonna 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. I'm gonna 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 we 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 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. Let me give it 2, 3 as arguments. And there's our matrix. Wait. Who stole 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 wanna catch two results, then we need to provide two variables to catch them, like this. I'm gonna give variables x and ss. And we'll do 2, 3 again. There. Looks like x got the matrix and ss got the sum. Now you see this 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 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 the negatives work. Now MATLAB complains. Error using myRand. Too many output arguments. Well, sorry MATLAB, we'll try not to do that in the future. [MUSIC] [APPLAUSE]