A Method So Easy, It Calls Itself
You know how to write methods, and you know how to call methods. When you write methods, you’ll usually fill them with more method calls. You can make methods, and those methods can call methods…
You can write a method that calls itself.
That’s recursion.
Well, on the surface, it’s an absurd idea; if all a method did was call itself, which would call itself again, it would loop like that forever. (Although this isn’t technically a loop, it’s similar to one; you can usually replace loops with recursion if you want.) But of course, it could do other things as well and maybe call itself only some of the time.
Recursion is particularly well-suited for situations where solving part of the problem “looks like” ...
Get Learn to Program, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.