Recursion

Recursion

Have you ever had a situation where there are two mirrors facing each other? If so, you should've noticed that each mirror have infinite, shrinking images of the other side, right? That is recursion! Another fun way to explain this is that you got a mysterious box. Someone told you that there's treasure in this box. You found out that there are two boxes in the box. You open one, which is empty. You open the other one, and found two more smaller boxes. You open one, and found two more smaller boxes. You open one again, and found the same thing: two more smaller boxes. This makes you frustrated, but the person who told you there's treasure told you to be patient, so you started once again, opening and opening boxes, always finding the same thing: two smaller boxes. Finally, you found the treasure after you had repeated twelve times of opening boxes! To explain simply, it is literally calling yourself. You can write a code like this:

Define a print_ten_times function. You have a parameter -  . Each time, you use an if to check if   is smaller than ten. If so, you print something, and then you write a line of code that says print_ten_times(++i). This is a recursion. You called yourself in a function. You can try it out. It should print something ten times.

It's easier than it sounds, right? Although I'm not sure if you understood it as clearly as possible. It's hard to explain it in such a short passage, without any pictures to help. So, you can search online if you don't understand completely.