Have you ever wondered what would happen if a function called itself inside of its body? This concept is called recursion. Recursion is solving a problem by having a function call itself over and over again. Each time the function is called, the problem is reduced. When the problem is reduced to a certain size, also known as its base case, we just solve it.

You often use and see recursion going on in real life without even realizing it. Here is an example of figuring out how many of a factor a number has.

Example: Let’s say the number is 24 and we want to figure out how many 2’s it has. One way is to use recursion. Starting with 24, we keep dividing it by 2 and keeping track of how many 2’s divided so far until we get a number not divisible by 2:

START→24 and zero 2’s divided so far→ 12 and one 2’s divided so far→ 6 and two 2’s divided so far→ 3 and three 2’s divided so far→STOP

-See that we had to divide by three 2’s to get from 24 to a number that is not divisible by 2. So the answer is 3.