How do nested functions work in MATLAB?

So, I apply to the task and you need to find out how the nested functions work . To be more specific, I would like to know exactly how the following gnovice example works .

The question arises:

Given the following function, what will be the output when entering the code in the command window?

function fcnHandle = counter value = 0; function currentValue = increment value = value+1; currentValue = value; end fcnHandle = @increment; end f1 = counter(); f2 = counter(); output = [f1() f1() f2() f1() f2()]; %# WHAT IS IT?! 

I do not pretend to work, and I can find out the answer to the question. I also found the answer from Mohsen to this intuitive question (find the size of the matrix without calling the built-in functions). However, I cannot help but hear the voice of Albert Einstein in my head.

enter image description here

I think the documentation was a bit dirty, so I would be very happy if someone could explain how this works.

+5
source share
3 answers

Let me try ....

Each time the function called counter() creates a unique function handle. This function descriptor is a function descriptor called increment, which takes its variable value and increments it by one.

So if it is called twice, as in the code (f1, f2), each time the same function (increment) is returned, but different descriptors. You have the TWICE function installed. And each of them now works independently. Since this specific function (increment) is used in the stored internal value to calculate it, you can observe how, if you call, as in your code example, the outputs [1 2 1 3 2].

A good way to better identify differences is to redefine the function as:

 function fcnHandle = counter(val) value = val; function currentValue = increment value = value+1; currentValue = value; end fcnHandle = @increment; end 

and calling it the following:

 >> f1 = counter(0); >> f2 = counter(1000); >> output = [f1() f1() f2() f1() f2()] %# WHAT IS IT? 

Now you will see that the output is [1 2 1001 3 1002]

Now makes more sense?

This code is created, so it uses this function descriptor attribute (it is the same, but it is copied twice).

+4
source

This use of nested functions is relevant for memoization (for additional reading, see using nested functions to memorize expensive functions ), since it uses parametric functions that process which save the value of the parameters when they are created.

You should notice two things:

  • counter() returns the function handle directly to the nested function

     f1 = counter() f1 = @counter/increment 
  • A nested function will โ€œsaveโ€ the changed areas. See functions() more details.

     s = functions(f1) s = function: 'counter/increment' type: 'nested' file: '\\ic.ac.uk\homes\ok1011\MATLAB\counter.m' workspace: {[1x1 struct]} 

    with saved workspace:

     s.workspace{1} ans = fcnHandle: @counter/increment value: 0 

Basically, counter() initializes value to zero, and consecutive calls to @counter/increment will do value = value+1; .

Finally, @counter/increment is assigned to f1 , and all f1() does @counter/increment() on the previously initialized value . Initializing f2 = counter() creates another counter with a separate saved workspace .

+3
source

Neat exercises. I've never used nested functions with descriptors before, so here is my thought process: I found this paragraph in the manual very instructive:

Nested functions can use variables from three sources:

 Input arguments Variables defined within the nested function Variables defined in a parent function, also called externally scoped variables 

When you create a function descriptor for a nested function, this descriptor stores not only the name of the function, but also the values โ€‹โ€‹of variables with external visibility.

So, in the example, the counter function sets the variable value to 0 , then calls increment . increment stores value in its descriptor, so every time increment is called, value incremented and overrides the first definition value=0 .

As a result, the output is 0 plus the number of times increment that this counter instance is called each time.

When you write this:

 f1 = counter(); f2 = counter(); 

You create two separate instances of the counter function, which will grow independently.

+2
source

Source: https://habr.com/ru/post/1204187/


All Articles