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
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)]); nested1(); disp(['Variable A in ''main'' is: ', num2str(A)]); function nested1() disp(['Variable A in ''nested1'' is: ', num2str(A)]); nested2(); function nested2() disp(['Variable A in ''nested2'' is: ', num2str(A)]); A = 3; end end end
In the preceding code,
main is the main function, 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
% Call the function 'main' main();
Variable A in 'main' is: 1 Variable A in 'nested1' is: 1 Variable A in 'nested2' is: 1 Variable A in 'main' is: 3
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.
I recommend the following books to learn more on functions in MATLAB.
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.
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.
- How to Define Functions in MATLAB
- How to Call a Function in MATLAB
- How to Add Help Documentation to a Function in MATLAB
- Local Functions in MATLAB
- Nested Functions in MATLAB
- Visibility of Nested Functions in MATLAB
- Private Functions in MATLAB
- Anonymous Functions in MATLAB
- Inside the Handle of Anonymous Functions in MATLAB
- Functions with Variable Input Arguments in MATLAB
- Functions with Variable Output Arguments in MATLAB
- How to Validate the Number of Input Arguments Passed to a Function in MATLAB
- How to Validate the Number of Output Arguments Passed to a Function in MATLAB
I hope you have learned what nested functions are in MATLAB. The source code developed in this tutorial is available at this page.