In very general terms, think of it as building up to a solution versus working your way down to one.
The classic example in programming for recursion is the factorial function. A! = A x (A-1) x (A-2) x … x 1.
An iterative solution would start with one and work up. X factorial is 1, times 2, times 3… up to X. “Do the multiplying thing X times” is the iteration.
A recursive solution starts from the top and works down to an end-case. Here, it’s 1! = 1. So what is X factorial? It’s X times (X-1) factorial. What’s (X-1) factorial? It’s (X-1) times (X-2) factorial… until you get to 1. “Call this same function with one less than before” is the recursion.
Latest Answers