RECURSION
Hi, I’m joseph David Funsho (off-light). I’m currently enrolled as an Alx software engineering student and I have decided to share some of the knowledge I have acquired during the program.
A few days ago we were introduced to a concept called RECURSION. We were also assigned some tasks to solve using the Recursion method.
Whats Recursion?
Recursion is a method used in programming languages that involve a function calling itself repeatedly until a certain condition is met. It is a powerful tool used in problem-solving, and it can be used to solve many types of problems.
Recursion works by breaking a problem into smaller and simpler sub-problems that can be solved more easily. The solution to the original problem is then derived from the solutions to the sub-problems.
Recursion is an important concept in computer science, and it is used in many different areas. It can solve problems involving mathematical computation, searching and sorting data, and manipulating strings. It can also be used to solve complex problems such as artificial intelligence and graph theory.
A recursion is a powerful tool, but it can also be tricky to implement correctly. To use recursion correctly, it is important to have an understanding of the problem and to know the base case.
The base case is the point at which the recursive function stops calling itself and returns a result. Once the base case is reached, the recursion can be terminated and a result can be returned.
Loop vs Recursion
when solving a problem it can either be solved iteratively (loop) or recursively. We will find the factorial of a number using iterative and recursive approaches.
Factorial of number 6 iteratively;
Finding the factorial of number 6 iteratively goes like this;
6! = 6 × 5 × 4 × 3 × 2 × 1
6! = 6 × 5 × 4 × 3 × 2
6! = 6 × 5 × 4 × 6
6! = 6 × 5 × 24
6! = 6 × 120
6! = 720
Get Joseph David Funsho (off-light)’s stories in your inbox
Join Medium for free to get updates from this writer.
Implementing the iterative approach in C programming language.
Factorial of Number 6 Using Recursive Approach;
Finding the factorial of number 6 recursively goes like this;
6! = 6 x 5!
5! = 5 x 4!
4! = 4 x 3!
3! = 3 x 2!
2! = 2 x 1!
1! = 1 x 0!
0! = 1.
we can see from the example above that the relationship is n! = n x ! (n — 1)
Implementing the Recursive Approach using C programing language.
Why Should I Use Recursion?
- It makes the syntax of your code simple, clear, and Readable.
- Recursion is useful when dealing with a complex problem that has a similar sub-problem.
The disadvantage of Recursion
- it is slower compared to loop
- it can easily lead to stack overflow.
thanks for reading.
Twitter: @OFF_WURLD
email: dfunsho72@gmail.com




