MATLAB: Calling the same function from within the function?

functions calling themselvesMATLAB

So I ran into this very interesting file from the file exchange…
Where the function calls itself around line 140 (its between 138 thru 144 as I started to play with some things…). This was wild to me. I have never attempted this and was wondering if there are other examples out there.
Does this work for C++ or Fortran?
Comments? Why is this blowing my mind?

Best Answer

This is called "recursion", and it is legal in most programming languages.
Every linear procedure can be rewritten as a recursive procedure.
Every recursive procedure can be rewritten as a linear procedure, provided that sufficient dynamic memory allocation is permitted. Doing so might be awkward and might require a lot of extra memory, but it is always possible.
There is a subset of recursion called "tail recursion" that can be quite efficient to execute.
Recursive functions are very important in theoretical computer science. The theory of what can be deterministically computed talks about "Primitive Recursive Functions"