DEV Community


Posted on

Difference Between Recursion and Iteration

What is the difference between recursion and iteration? Both recursion and iteration are running instruction repeatedly. However, they are totally different. At the first time I was also a little bit confuse. But after read some article and reference, finally I understand. I will summarize it for you and make it very simple to be understood.

Recursion is call a function from inside it's function. This will make a repetitive process.

We can take example a film titled "Inception" as a recursion process. In this film, the actor Leonardo DiCaprio as Dominic Cobb, and his team must go inside into someone's dream. If the mission is not succeed, they must go into deeper dream. And it happen repeatedly until they accomplish the mission.

So recursion is somehow go deeper inside by repeatedly doing such a function from inside the function.

Next, what is iteration? Even though it has similarity with recursion in term of repeated. But it has different meaning. Iteration is doing instruction repeatedly within a function.

For example, when our mom are making soup with love, she cuts the carrots repeatedly until they all be cut. So, she will chop the carrots for more than once, in order to make delicious soup.

I hope my explanation can be understood in a very easy way.

Top comments (0)