CMSC 201

Lab 11: Recursion

Recursion

Recursion is a powerful tool that allows you to write code that solves a problem by breaking it up into smaller problems that you would all solve the same way, working towards a base case where you can give a definitive answer and use that information to solve continually bigger problems.

Some tips for Recursion:

  • Think about how you could make the problem smaller. i.e. make the amount of data you have to process smaller, make numbers smaller, etc.

  • What are you going to have to do over and over again? And when do you stop? When you stop is going to determien your base case(s), and what you do over and over again is the general case.

  • What can you do to make the problem smaller? Remove items from a list? Divide a number? This is what you will do before calling the same exact function agian, but with the new, smaller problem.

  • Most of all: don't just write, think! Before diving into writing code, give this problem some thought. The same goes for future homework assignments and project; you should always read through the instructions, unerstan the problem at hand, and plan out your solution first.