Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are multiple functions in one .m file nested or local when "end" isn't used

In MATLAB you can have multiple functions in one .m file. There is of course the main function, and then either nested or local functions.

Examples of each function type:

% myfunc.m with local function ------------------------------------------
function myfunc()
    disp(mylocalfunc());
end
function output = mylocalfunc()
    % local function, no visibility of variables local to myfunc()
    output = 'hello world';
end
% -----------------------------------------------------------------------

% myfunc.m with nested function -----------------------------------------
function myfunc()
    disp(mynestedfunc());
    function output = mynestedfunc()
        % nested function, has visibility of variables local to myfunc()
        output = 'hello world';
    end
end
% ----------------------------------------------------------------------

The difference is clear when you use the functions' end statements. However, I don't think it's clearly documented which you are using when you don't, since this is valid syntax:

% myfunc.m with some other function 
function myfunc()
    disp(myotherfunc());
function output = myotherfunc()
    % It's not immediately clear whether this is nested or local!
    output = 'hello world';

Is there any clear definition of whether functions written like myotherfunc are local or nested?

like image 414
Wolfie Avatar asked Sep 13 '17 14:09

Wolfie


People also ask

Can you have multiple functions in one MATLAB file?

You can write n number of functions in a single m file.

What are nested functions MATLAB?

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.

Is it bad practice to use nested functions?

no, there's nothing wrong with that at all, and in js, it's usually a good thing. the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling.

Can a function be nested inside another function?

Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct access to variables and names defined in the enclosing function.


1 Answers

This can be quickly tested because of the variable scope differences mentioned in the documentation

The primary difference between nested functions and local functions is that nested functions can use variables defined in parent functions without explicitly passing those variables as arguments.

So adapting the question example:

function myfunc()
    % Define some variable 'str' inside the scope of myfunc()
    str = 'hello world';
    disp(myotherfunc());
function output = myotherfunc()
    % This gives an error because myotherfunc() has no visibility of 'str'!
    output = str;  

This errors because myotherfunc is in fact a local function, not a nested function.

The test is supported by the documentation for nested functions which states:

Typically, functions do not require an end statement. However, to nest any function in a program file, all functions in that file must use an end statement.

like image 83
Wolfie Avatar answered Oct 20 '22 18:10

Wolfie