Start a Project

Swift Recursion

In this blog, we are going to learn about recursion and how Swift Recursion works in swift with the help of an example.

First of all, I want to tell you about the recursion. The function that calls itself is known as the recursion function.

The working of the recursion function

The recursion function works in two cases.

1. To call the recursion function.

2. Stopping condition

Note:

If we do not put any break condition then the recursive function continuously calls itself infinitely.

We need to use any (if…else) similar type of approach to breaking the condition.

Advantages of Recursion

It makes code shorter and cleaner

Mainly used in graph and tree traversal.

Disadvantages of Recursion

Debugging is not easy as compared to the iterator program.

Takes more stack space.

Takes more time in processing.

Now lets us understand Swift Recusrion with a simple example

Output Image

Conclusion

I hope this blog helps you to understand the Swift Recursion.

Thanks for reading !!

For more blogs click here

Exit mobile version