Recursion
From Wikipedia - The Sierpinski triangle—a confined recursion of triangles that form a fractal

Recursion

Recursion: What is it?

Recursion occurs when a thing is defined in terms of itself or of it's type. In programming this occurs when a function calls on itself (directly or indirectly) - a recursive call of the function. Below we see a flowchart showing how a recursive function would work

No alt text provided for this image

To write a recursive function we need a base case or statement and the recursive call. The base condition will allow us to stop the recursion once this condition is met.

No alt text provided for this image

Let's see how this would work:

No alt text provided for this image

Starts off with our main function calling on our pow recursive function. In our recursive function we find our base condition which is when it'll stop the recursion and our recursive call. As we can see from the picture, the function will keep calling on it self, forming a stack, till it reaches the base condition of y == 0.

No alt text provided for this image

At this point it will start returning on itself to resolve it.

No alt text provided for this image

Therefore we end up with 2 to the power of 2 being equal to 4, by the power of recursion!

Hope this helped with furthering understanding how recursion in programing works.

Till next time!

To view or add a comment, sign in

Explore content categories