Programming |

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
- Calling Nested Functions
- Variable Scope in Nested Functions
- Using Function Handles with Nested Functions
- Examples of Nested Functions

**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`

:

Primary M-File Functions | Calling Nested Functions |

© 1994-2005 The MathWorks, Inc.