Recursion

Recursion: writing a function that calls itself on a smaller input, tracing it through the call stack, and replacing it with a loop when it runs too deep.

  • 8 cards
  • 5 questions
  • About 23 min
  • Beginner to Intermediate
Share

A recursive function solves a problem by calling itself on a smaller version of it, until the problem is small enough to answer directly. It matters because many structures are made of smaller copies of themselves, from folders to the trees that follow this topic, and recursion is the most direct way to write code for them, while also being the easiest way to run out of stack. The topic starts with the base case, the recursive case and the call stack that holds a frame for every unfinished call, then traces a function that works on both sides of its call. It then writes recursive functions from a definition, on numbers and on nested folders. It ends with the base case that is never reached, the error each language raises when the stack fills, and the loop with an explicit stack that replaces a recursion too deep to run.

Contents

8 cardsnot started