Nested Functions in MATLAB

Functions contained within another function are called “nested functions”.


Any function in a program file can contain a function or a set of functions. The container function is called the “parent function”, and contained functions are called “nested functions”.

Nested functions can also contain other functions, which generates a hierarchy. The main advantages of this type of function are:

  • Nested functions can access the variables of their parents in the hierarchy
  • Nested functions have a limited visibility
Learn more on the visibility of nested functions here.

Use nested functions when you organize the logic of a complex function (the parent function) in one or more functions (the nested functions), and they share data. If you use other type of function, you will need to pass the data as input arguments.


Define nested functions in the same way as any other function, but in a parent function.

Remember that nested functions can access the variables of all their parents in the hierarchy. If the parent functions do not use a variable present in the body of a nested function, the variable remains local to the nested function.

function main()
A = 1;
disp(['Variable A in ''main'' is: ', num2str(A)]);
disp(['Variable A in ''main'' is: ', num2str(A)]);
    function nested1()
        disp(['Variable A in ''nested1'' is: ', num2str(A)]);
        function nested2()            
            disp(['Variable A in ''nested2'' is: ', num2str(A)]);
            A = 3;

In the preceding code, main is the main function, and nested1 and nested2 are two functions nested at different levels. If you call main, the output evidences that the nested functions have access to the variable A declared in main.

The file ‘main.m’ must be in the current directory or in the search path.
% Call the function 'main'


Local functions and nested functions have precedence over functions in other files, but nested functions have higher priority.

This fact means that when you call a function within a program file, MATLAB checks if the function is a nested function in the current scope, and next if it is a local function. If no coincidence is found, then MATLAB searches the function in other files.

Further reading

I recommend the following books to learn more on functions in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a Textbook Excellence Award. The sixth chapter covers user-defined functions, scope of variables, debugging, and other aspects related to the development of MATLAB programs.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 6 explains how to create functions, including nested functions, anonymous functions, and local functions.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

web(fullfile(docroot, 'matlab/matlab_prog/nested-functions.html'));

Source code

I hope you have learned what nested functions are in MATLAB. The source code developed in this tutorial is available at this page.


Your friends may need this knowledge to complete a task. Share this tutorial.