Programming

Variable Scope in Nested Functions

The scope of a variable is the range of functions that have direct access to the variable to set, modify, or acquire its value. When you define a local (i.e., nonglobal) variable within a function, its scope is normally restricted to that function alone. For example, subfunctions do not share variables with the primary function or with other subfunctions. This is because each function and subfunction stores its variables in its own separate workspace.

Like other functions, a nested function has its own workspace. But it also has access to the workspaces of all functions in which it is nested. So, for example, a variable that has a value assigned to it by the primary function can be read or overwritten by a function nested at any level within the primary. Similarly, a variable that is assigned in a nested function can be read or overwritten by any of the functions containing that function.

In the following two examples, variable `x` is stored in the workspace of the outer `varScope` function and can be read or written to by all functions nested within it.

 `function varScope1x = 5;nestfun1   function nestfun1      nestfun2      function nestfun2         x = x + 1      end   endend` `function varScope2nestfun1   function nestfun1      nestfun2      function nestfun2         x = 5;      end   endx = x + 1end`

As a rule, a variable used or defined within a nested function resides in the workspace of the outermost function that both contains the nested function and accesses that variable. The scope of this variable is then the function to which this workspace belongs, and all functions nested to any level within that function.

In the next example, the outer function, `varScope3`, does not access variable `x`. Following the rule just stated, `x` is unknown to the outer function and thus is not shared between the two nested functions. In fact, there are two separate `x` variables in this example: one in the function workspace of `nestfun1` and one in the function workspace of `nestfun2`. When `nestfun2` attempts to update `x`, it fails because `x` does not yet exist in this workspace:

• ```function varScope3
nestfun1
nestfun2

function nestfun1
x = 5;
end

function nestfun2
x = x + 1
end
end
```

The Scope of Output Variables

Variables containing values returned by a nested function are not in the scope of outer functions. In the two examples shown here, the one on the left fails in the second to last line because, although the value of `y` is returned by the nested function, the variable `y` is local to the nested function, and unknown to the outer function. The example on the right assigns the return value to a variable, `z`, and then displays the value of `z` correctly.

 Incorrect Correct `function varScope4x = 5;nestfun;   function y = nestfun      y = x + 1;   end``yend` `function varScope5x = 5;z = nestfun;   function y = nestfun      y = x + 1;   end``zend`

 Calling Nested Functions Using Function Handles with Nested Functions