MATLAB: Location of the “end” statement of the main function in a m. file

endlocal functionmain function

Hello,
could someone write what is the difference between possible location of the "end" statement of the main function: before first local function vs end of a file? What is the difference?
For example: first vs. second case in this answer.
Thank you!
Marek

Best Answer

"before first local function"
any functions defined after the main function are local functions:
"vs end of a file?"
any functions defined within the main function are nested functions:
These can be combined too, e.g.:
function mymain()
... code
function mynest()
... code
end % mynest()
end % mymain()
function mylocal()
... code
end % mylocal()
Nested functions have access to the parent function's workspace. Local functions do not.