[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 saved into Bob.m,
and a function Mary, which we saved into 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 want to 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.
We are going to get rid of this.
Instead of calling sum here, we're going to call
sumAllElements
of “a”.
Now, that's not a built-in function, so where is it going to come from?
Well, we are going to put it right here.
Let me get rid of this end statement.
[CLICKING]
We're going to put it right here
[CLICKING]
in the same file.
[CLICKING]
I got rid of the end statement because if you have
more than 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 of 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 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 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 myRand,
“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 sumAllElements from inside myRand,
we use “a” as an input argument to pass its value into sumAllElements.
That value, which happens to be a three by four array, is copied from
“a” into the input argument m inside sumAllElements.
Note that we calculated the sum of the elements of the three by four array, which
is in M, the same way as we did 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 other functions.
Okay. So we passed 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, then
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]