Nested Function

A nested function—strictly, an inner-nested function—is a function defined entirely inside another function. A nested function may itself contain nested functions.

Requirements

  1. In any file that contains a nested function, every function definition must end with end
  2. A nested function can appear anywhere within its parent’s body (it is no longer required to be last), but cannot appear inside any Matlab Control Statements

Calling rules involving nested functions:

  • An outer function can call functions nested one level inward, but cannot call functions at deeper levels
  • A nested function can call other nested functions at the same level that share the same parent
  • A nested function can also call any of its ancestor functions at any depth, or other nested functions sharing one of those ancestors as parent — but it cannot reach into nested functions that are themselves more deeply nested inside such siblings

Sharing Variables

  • A nested function can directly use any variable defined in any of its ancestor functions, provided that variable was not defined inside a sibling nested function

Examples

A

A valid example:

function A()
B(); D();
    function B()
    C(); D();
        function C()
        B(); D();