nestedFunctions()

Programming

nestedFunctions() is a MATLAB®-compatible programming function for a nested function is defined inside another function's body using a normal "function ... end" block. it closes over the enclosing function's local variables — reading them at call time and writing mutations back to the parent on return. only variables that already existed in the parent scope before the call are written back; new variables created inside the nested function remain local. formal parameters of the nested function shadow (but do not corrupt) identically-named parent variables. limitation: the nested function must be defined (its "function" line executed) before it is called; forward references are not supported. Run it online in your browser with SimLab — free, no install, no license required.

A nested function is defined inside another function's body using a normal "function ... end" block. It closes over the enclosing function's local variables — reading them at call time and writing mutations back to the parent on return. Only variables that already existed in the parent scope before the call are written back; new variables created inside the nested function remain local. Formal parameters of the nested function shadow (but do not corrupt) identically-named parent variables. Limitation: the nested function must be defined (its "function" line executed) before it is called; forward references are not supported.

Syntax

function outer()
  count = 0;
  function increment()
    count = count + 1;
  end
  increment();
end

Examples — run nestedFunctions() in your browser

% Canonical counter — nested fn mutates parent local
function result = makeCounter()
  count = 0;
  function increment()
    count = count + 1;
  end
  increment();
  increment();
  result = count;  % 2
end
r = makeCounter()
▶ Run

See Also

Frequently Asked Questions about nestedFunctions()

Is the nestedFunctions() function free to use in SimLab?

Yes. SimLab is 100% free — no signup, no license, and no usage limits. You can run nestedFunctions() and 510+ other MATLAB®-compatible functions directly in your browser at simulations4all.com/simlab.

Do I need to install MATLAB to use nestedFunctions()?

No. SimLab runs entirely in your web browser with zero installation. The nestedFunctions() function works without any MATLAB® license or software download.

What does nestedFunctions() do?

A nested function is defined inside another function's body using a normal "function ... end" block. It closes over the enclosing function's local variables — reading them at call time and writing mutations back to the parent on return. Only variables that already existed in the parent scope before the call are written back; new variables created inside the nested function remain local. Formal parameters of the nested function shadow (but do not corrupt) identically-named parent variables. Limitation: the nested function must be defined (its "function" line executed) before it is called; forward references are not supported. It is a MATLAB®-compatible programming function available in SimLab with the same calling syntax as MATLAB® — typically: function outer() count = 0; function increment() count = count + 1; end increment(); end.

Can I run nestedFunctions() in my browser?

Yes. Open SimLab at simulations4all.com/simlab, write or paste code that calls nestedFunctions(), and run it. Execution happens locally in your browser — no data leaves your machine.

Try SimLab — MATLAB®-compatible, free, in your browser

510+ functions. Runs in your browser. No install. No license.

Open SimLab

MATLAB® is a registered trademark of The MathWorks, Inc. SimLab is an independent project by Simulations4All and is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

Stay Updated

Get notified about new simulations and tools. We send 1-2 emails per month.