So, I'm applying for a job and need to figure out how nested functions work. To be more specific, I would like to know exactly how the following example posted by gnovice works.
The question is:
Given the following function, what will the output be when entering the code below 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'm not applying for a job, and I'm able to figure out the answer to the question. I also find the answer from Mohsen to this question intuitive (find the size of a matrix without calling built-in functions). However, I can't help but hearing Albert Einstein's voice in my head.
I think the documentation was a bit messy, so I would be very happy if someone is able to explain how it works.