3 ms·
Actually, this is not correct. While recursion is typically implemented in the form of a function which calls itself, that is not exactly what recursion means.
by athom 15y ago
Actually, this is not correct. While recursion is typically implemented in the form of a function which calls itself, that is not exactly what recursion means. In fact, "recursion" derives from "recur", which simply means to repeat, or re-occur, which, when you look at it, is exactly what a "recursive" function is designed to do (without all that messy for/next, do/while jazz). Calling itself is just a particularly clever, interesting, and elegant way to do that.
To really illustrate the point of recursion, consider showing your students one of the simplest recursive algorithms out there. You can find it on the back of many shampoo bottles:
* Lather.
* Rinse.
* Repeat.
- rimantas 15y agoSound like your definition of recursion fits any loop. I doubt that's what recursion means in CS.
- athom 15y agoThis is why most looping structurues can be replaced with recursion. It's also why you hear of "unrolling" tail-recursive calls into more traditional looping structures. They're equivalent.
- merijnv 15y agoThat would be because recursion and loops are isomorphic. Anything you can do with recursion can be done in a loop and vice versa. It's just that one or the other may be clearer or easier depending on what you're doing.