What is Recursion?
Dart Recursion is the method where a function calls itself as its subroutine. It is used to solve the complex problem by dividing it into sub-part. A function which is called itself again and again or recursively, then this process is called recursion.
The iterators can be an option to solve problems, but recursion is recommended to the programmers to deal with complex problems because it is an effective approach of problem-solving technique. It requires less time and code to evaluate the same complex task.
Recursion makes many calls to the same function; however, there should be a base case to terminate the recursion.
Recursion uses the divide and conquers technique to solve a complex mathematical computation task. It divides the large task into small chunks.
Recursion is not recommended to solve all types of problems. However, it is best for a few questions such as searching, sorting, Inorder/Preorder/Postorder, Tree Traversal, and DFS of Graph algorithms. But, while using recursion, it must be implemented carefully; otherwise, it turns into the infinite loop.
What is base condition in recursion?
In the above example, the base case is defined as n<=1, and a larger value of a number can be solved by changing to a lesser one till the base case is matched.
Note - The base case or valid terminating condition is required in recursion function; otherwise, it will turn into an infinite loop.
Dart Recursive Function
Recursive functions are quite similar to the other functions, but difference is to calling itself recursively. A recursive function repeats multiple times until it returns the final output. It allows programmers to solve complex problems with minimal code.
How does recursion works?
Let's understand the concept of the recursion of the example of a factorial of a given number. In the following example, we will evaluate the factorial of n numbers. It is the series of the multiplication as follows.
Characteristics of Recursive Function
The characteristics of the recursive function are given below.
Let's have a look at recursion syntax:
Let's understand the following example.
Example - 1
Factorial Of 10 is: 120
In the above example, the factorial() is a recursive function as it call itself. When we called the factorial() function by passing the integer value 5, it will recursively call itself by decreasing the number.
The factorial() function will be called every time until it matched the base condition, or it is equal to one. It multiplied the number with factorial of the number. Consider the following explanation of the recursive call.
The recursion is ended when the number reduced to 1, and it is the base condition of recursion.
A recursion function must have a base condition to avoid to infinite call.
Disadvantage of Recursion