EDDYMENS

Published 9 months ago

What Is Recursion In Programming?

Table of contents

Definition

Recursion is the process of creating a looping effect by allowing a function to call on itself over and over till a condition is met.

Example

01: function sum(number) { 02: if (number===0) { 03: return 0; 04: } 05: return number + sum(number-1); 06: } 07: 08: sum(10);

In the sample code above the sum function finds the sum of 1,2,3.. through to 10, it does this by recursively calling itself on line 05 by attempting to find the sum of the number and number - 1 it does this till there are no numbers left, which is checked on line 02.

Summary

As you can see recursion is not as straightforward as loops and depending on the operation, the function might look more complex that it should be.

Here is another article you might like 😊 What Is A Passphrase