What Are Nested Functions?
A nested function is a function that is completely contained within a parent function. Any function in a program file can include a nested function.
For example, this function named parent contains a nested function named nestedfx:
1 2 3 4 5 6 7 8 9 | function parent disp('This is the parent function') nestedfx function nestedfx disp('This is the nested function') end end |
The primary difference between nested functions and other types of functions is that they can access and modify variables that are defined in their parent functions. As a result:
- Nested functions can use variables that are not explicitly passed as input arguments.
- In a parent function, you can create a handle to a nested function that contains the data necessary to run the nested function.
No comments:
Post a Comment