4 ms·
I remember what made me click was using a bit of assembly. If you imagine your code as a sequence of instructions, recursion is basically a `JMP` to the start
by Draiken 7y ago
I remember what made me click was using a bit of assembly.
If you imagine your code as a sequence of instructions, recursion is basically a `JMP` to the start of the stack again, normally with different values.
Once a condition is met, instead of jumping back to the start, it returns something. That condition then repeats itself inside all the repetitions eventually returning the value to the original caller.
# imagine we're recursing to subtract to zero
1. call function subtract with 10 (set x = 10)
2. subtract x by 1
3. if zero, return 0
4. else return the result of calling the function with x-1 (jump to 2)
Once I realized deep down at the CPU level it's all a sequence of instructions, recursion is nothing but moving the pointer. With higher level languages there's a bit more involved, but the gist is the same.