Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Have you ever thought about how we solve complex problems by breaking them into smaller blocks of code using recursion? How Recursion helps in reducing the time in writing code?
In this article, we will discuss about recursion in Carbon language. We will see Fibonacci series using recursion. We will also learn about the advantages and disadvantages of recursion while using it. Moving forward, let’s first understand what exactly recursion is.
What is Recursion?
A complex problem can be solved easily when it breaks down into smaller sub-problems. Recursion allows breaking problems into smaller sub-problems, which are solved using recursive calls. In recursion, a function calls itself till the base condition is met for solving the problem.
What is a Recursive Function?
A function that calls itself until the program gets the required result is called a recursive function. Let’s understand the rules for writing code using recursion.
Rules of Writing Recursion
In recursion, a function must call itself to make recursive calls.
Recursion must have a base condition in order to finish the execution of a program.
Using recursion, the recursive calls must change their state and move toward the base condition in order to solve the problem.
Syntax
fn recursiveFunction() —------ Recursive function
{
BaseCase; —------ Base Condition
recursiveFunction(); —------ Recursive call
}
fn Main() -> i32
{
recursiveFunction(); -—------ Function call
}
Let’s understand recursion with the help of an example of Fibonacci series. This series is made by adding two previous numbers. The first and the second numbers are fixed, and they are 0 and 1.
Explanation
Let’s suppose we want to print the 10th number of the Fibonacci series. In this program, recursive calls will be made until the value becomes 0 or 1. If the number becomes 0 or 1, then it will be returned otherwise, recursive calls for (n-1) and (n-2) will be made.
The base condition is the stopping condition of recursive calls. Base condition is necessary for recursion as it allows the program not to execute infinitely.
Why recursion is slow in solving problems?
Recursion is slow because it takes time in storing all the recursive calls in a stack and then pop them out according to the call order.
What is the difference between recursion and iteration?
Recursion code is smaller than iterative code, but recursive calls take time to solve problems and are also not easily understood as compared to iterative code.
Why recursion takes more memory?
Recursion takes more memory because of recursive calls, with each recursive call, the function is added to the stack, and the values will stay there until all the calls are not finished.
Why recursion is required?
Recursion helps in breaking complex problems into smaller sub-problems which makes them easy to read and understand than an iterative solution.
Conclusion
In this article, we have understood recursion in the Carbon language. We have seen rules for writing recursion code while programming. We have made a code of the Fibonacci series using recursion in Carbon language. We have discussed the advantages and disadvantages of using recursion. If you want to read more about recursion and Carbon language, then you can refer to the below articles.
I hope this article helped you understand recursion in Carbon. If you want to read more articles, then you can visit our platform Coding Ninjas Studio. You can also prepare for tech interviews and tech exams through our platform. Coding Ninjas provides company-wise preparation guides which will help you in enhancing your knowledge. Our platform also provides topic-wise coding questions so you can practice those questions and get placed in good companies.