smkelsey wrote: |
---|
Ive never seen a function call within its same function. |
it's called
recursion and has the following features:
1. in one case, it will call itself with a new argument (the recursion: Line 3)
2. otherwise, it will return a result or do something
besides calling itself
in the stages where it's calling itself, the effect is same as pushing results onto a stack (1)
when it hits the end case (2), the stack unwinds
think about what happens when you call a function foo() - it acts the same way - the computer will call all the functions inside foo until it hits the end-brace; when that happens, you will pop back out of the function - it's the same with recursion
because a stack acts as LIFO, last-in-first-out, its net effect is the same as "reversing" your initial results
so with your initial code, had you pushed the digits onto a stack (instead of printing them out immediately) and then unwound the stack afterwards while printing, you would get the same result