[MUSIC] As we've seen, one function can call another function. For example, myRand called the built-in functions rand and sum. But built-in functions don't have to be involved at all. Our own functions can call each other as well. We could, for example, define a function called bob, which we save into bob.m, and a function mary, which we save in mary.m, and bob could call mary or mary could call bob. Furthermore, we can define multiple functions in the same M file and have them call each other. Why, you may ask, would we put two functions in the same file? Well, if the problem we're trying to solve is complicated, we'll wanna break it up into several functions. But if those functions are not likely to be useful for solving other problems, then it makes sense to keep them all in the same M file. Let's see an example. Let's rewrite the last version of myRand. Let's see. We'll leave all this the same. But we're going to get rid of this. Instead of calling sum here, we're gonna call sumAllElements(a). Now, that's not a built-in function. So where's it gonna come from? Well, we're gonna put it right here and get rid of this end statement. We're gonna put it right here. In the same file. I got rid of the end statement because if you have more then one function in a file, either they all have to have end statements or none of them can have end statements, and it's extra typing so I've just gotten rid of it. We've created a separate function for summing up the elements. This is, of course, a very simple example, and there's not much of an advantage to doing this here, but later we'll see examples when it matters. In any case, you can see how simple the syntax is, the second function keyword, right here indicates that the first function has ended, and the second one has begun. Functions that are defined in the same file like this behave in most respects just like other functions. The only difference is that only the first function up here, which is called the main function, can be called by other functions outside the file. The other functions, in this case we only have one, are called subfunctions or local functions, and they can only be called by functions inside the the file. Like other functions, all their variables are local, and this means that the variables in a function are unavailable to the command window and unavailable to all other functions, whether or not they're in the same file. So v is not available inside my Rand, a is not available inside sumAllElements, and none of these variables are available down here inside the command window. Well, let's see if this thing works. Let's see, we'll do the same exact command that we did before we made the change. There. Works. The only way to exchange information between these functions is with input and output arguments. In this case, when we call sumElements from inside myRand, we use a as an input argument to pass its value into sumAllElements. That value, which happens to be a 3 by 4 array, is copied from a into the input argument m inside sumAllElements. Note that we calculated the sum of the elements of the 3 by 4 array which is in m the same ways we did it before, but this time the calculation is done inside sumAllElements, and the array that we're working on is inside the variable m. Note that we can't use the variable a here inside sumAllElements. We can't because it's a local variable of myRand. And the local variables of myRand are unavailable inside of other functions. Okay, so we pass the array into sumAllElements via an input argument. And after we calculate the sum, we need to send that sum back to myRand. Well, inside sumAllElements, this local variable summa is declared right here to be an output argument. So that means when sumAllElements is done and closes, the contents of that summa, which is the sum we want, will appear right here in myRand, where sumAllElements was invoked. And then it'll be copied into s and when myRand ends that'll go back to the command window, or whoever called this function myRand. The passing of information via input and output arguments when one function calls another is the same for any function, whether it be a subfunction or not, and whether it's a built-in function or not. Okay, so that's how this thing works, you can put as many subfunctions in an M file as you want. But the first function, you should remember, is always the only one that can be called from outside. [MUSIC] [APPLAUSE]