Nested functions, MATLAB in Statistics

Nested Functions:

We have seen that the loops can be nested, that means that one inside of the other, functions can be nested. The terminology for nested functions is that an outer function can have within its inner functions. Whenever functions are nested, every function should have an end statement (as like loops). The common format of a nested function is as shown below:

outer function header

body of outer function

inner function header

body of inner function

end % inner function

more body of outer function

end % outer function

The inner function can be in any part of the body of outer function therefore there may be parts of the body of outer function before and after the inner function. Also, there can be multiple inner functions.

The scope of any variable is the workspace of outermost function in which it is defined and used. Which means that a variable is defined in the outer function can be used in an inner function. A variable is defined in the inner function can be used in the outer function, but when it is not used in the outer function the scope is merely the inner function.