__Introduction to Recursion__

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.

**Resources**:

__Previous Chapter__

__Next Section__

**Copyright ÂŠ 2021 Code 4 Tomorrow. All rights reserved.**
The code in this course is licensed under the MIT License.
*If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact **classes@code4tomorrow.org** for inquiries.*