Programming Previous page   Next Page

Nested Functions

You can define one or more functions within another function in MATLAB. These inner functions are said to be nested within the function that contains them. You can also nest functions within other nested functions.

This section covers the following topics on using nested functions in MATLAB:

Writing Nested Functions

To write a nested function, simply define one function within the body of another function in an M-file. Like any M-file function, a nested function contains any or all of the components described in Basic Parts of an M-File. In addition, you must always terminate a nested function with an end statement:

Example -- More Than One Nested Function

This example shows function A and two additional functions nested inside A at the same level:

Example -- Multiply Nested Functions

This example shows multiply nested functions, C nested inside B, and B in A:


Previous page  Primary M-File Functions Calling Nested Functions Next page

© 1994-2005 The MathWorks, Inc.