Table of contents
1.
Introduction
2.
Recursion
2.1.
Syntax
2.2.
Advantage of Recursion
2.3.
Disadvantage of Recursion
2.4.
Example
3.
Frequently Asked Questions
3.1.
How are recursive functions different from non-recursive functions?
3.2.
Why does the recursive function take more space?
3.3.
What will happen if we remove the base condition from a recursive function?
4.
Conclusion 
Last Updated: Mar 27, 2024

Dart Recursion

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Recursion. We will first look at recursion, and then we will see how we can use recursion in a Dart program. The syntax of a recursive function in Dart will also be discussed. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Recursion

In any computer language, Recursion refers to a function calling itself. A recursive function invokes itself repeatedly until a specific base condition is reached. Recursion can be used to break down substantial complex problems into smaller subproblems in order to solve them efficiently.

Two main parts of a recursive function are:

  1. Base Condition: Every time the function call is made, it checks for the base condition. If the base condition evaluates to true, the function returns to the last recursive call. 
  2. Recursive: If the base condition is not satisfied, the rest of the recursive code is rerun.

Syntax

The syntax of a recursive function in Dart is as follows:

void recrusive_function() {
  // Base condition to terminate the program

  recrusive_function();

  // some other code
}

void main() {
  recrusive_function();
}

Advantage of Recursion

Following are some of the advantages of using recursion in a program:

  1. It is helpful in breaking down a huge problem into smaller subproblems.
  2. Code for a recursive function is generally concise. 
  3. It reduces redundant function calls.

Disadvantage of Recursion

Following are some of the disadvantages of using recursion in a program:

  1. It generally uses a lot of space.
  2. It is slower than normal iterations because of the high stack overhead.
  3. It's slightly harder to debug a recursive function than a non-recursive function.

Example

Now that we have learned what recursion is, let's look at an example to understand the concept better:

// This program computes the sum of first n natural numbers
int sumUptoN(int n) {
  // return n if the value of n is less than or equal to 1
  if (n == 1) {
    return n;
  }
  // else return n + sum of numbers upto n-1
  return n + sumUptoN(n - 1);
}

void main() {
  int n = 5;
  // calling sumUptoN function to compute the sum of natural numbers upto n
  int sum = sumUptoN(n);
  print("The Total sum is: $sum");
}

Output:

The Total sum is: 15

Explanation:

The above program computes the sum of natural numbers up to N. It first checks whether the value of n is equal to 1 or not. This is the base condition for this recursive function. If the base condition is satisfied, it returns n. Otherwise, it returns n plus the sum of natural numbers up to n-1. 

Frequently Asked Questions

How are recursive functions different from non-recursive functions?

A recursive function functions call itself again and again. They usually take more space than the non-recursive ones and are slightly slower than iterations.

Why does the recursive function take more space?

It takes more space than that of an iterative function since the function must add to the stack with each recursive call, and the values are kept there until the call finishes.

What will happen if we remove the base condition from a recursive function?

On removing the base condition, the program will go to an infinite loop, and it will never stop executing.

Conclusion 

In this article, we have extensively discussed Recursions in Dart. We hope that this blog has helped you enhance your knowledge regarding Dart Recursion, and to learn more, check out our other article on Dart Strings. And also check out the awesome content on the Coding Ninjas Website,

Also check out - Rod Cutting Problem


Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass