Search code examples
matlabnested-function

Variables' scope of nested function in MATLAB


I am a little confused about the variables' scope in a nested function in MATLAB.

  1. As nested function help docs explains: vaiable remains local to the nested function.
function main
   nestedfun1
   nestedfun2

   function nestedfun1
      x = 1;
   end

   function nestedfun2
      x = 2;
   end
save('maindata')
end

Does 'remains local' mean variables are deleted after calling nested function? Or keeping them in nested local workspace?

My opinion: main function calls nestedfun1 and nestfun2, but variables are local to nested function, so after these 2 nested functions are called, variable x is removed (deleted), so maindata.mat is empty, right?

  1. Sharing variables between parent and nested functions.
function main2
nestfun2

   function nestfun2
      x = 5;
   end 
   
x = x + 1;
end

main2 could be run successfully, but what I don't understand is:

a) call of nestfun2 is before x=x+1, why main function could be run? There is no define variable x when calling nestfun2.

b) Which line does MATLAB parse the function to see what variable will be created? x=5 or x=x+1? x=5 is earlier but it's in nested function.

c) Variable remains local, after calls nestfun2, why variable x=5 could be transferred to x=x+1?

d) If x=x+1 is deleted, main2 could also be run successfully. But if it's changed to just x, main2 produces an error (“Identifier 'x' is not a function or a shared variable”), why?


Solution

  • Yes, a local variable exists only during the time the function runs (unless it’s labeled persistent). A new workspace is created every time a function starts running, and this workspace is destroyed after. Each function has its own workspace, so that variables with the same name in different functions can be kept separate.

    A nested function is a bit different in that it can access not only the variables in its own workspace, but also those in its parent’s workspace.

    MATLAB’s variable scope is a bit different from languages such as Python or C. In those languages, variables exist from the moment they’re created until the end of their scope. In MATLAB, they exist during the full run of the function. MATLAB first parses the whole function, and compiles it. So at the start of the function, it already knows that x will be defined later. Names cannot change function during the execution of a function. You cannot use e.g. cos both as a function and as a variable. Something like

    function bad
    cos(5)
    cos = 5;
    end
    

    produces an error because cos is understood to be a variable, not a function, during the full scope of the function. cos(5) is therefore indexing into a variable that has had nothing assigned to it yet.

    This is what causes main2 to work the way it does: x is known to be a variable in this function’s scope (in its workspace) at the start of the function. Calling the nested function assigns to this variable, not to a local one within the nested function’s workspace, because it is already known to exist in the parent’s workspace.