DEV Community

Cover image for Recursion, A Beginner's guide
ckorley4
ckorley4

Posted on

Recursion, A Beginner's guide

Intronduction
Recursion is used to solve problems that contain smaller sub-problems.When a function calls itself, recusion has occurred. A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).

Lets look at an example of traversing a complex object

const userInfo = {
  came: 'Thomas',
  school: {
    name: 'Flat Schools',
    jobTitle: 'Developer',
  },
  friends: [
    {
     name: 'Ama',
      school: {
        name: 'Carlifornia Low',
        jobTitle: 'Programmer',
      },
    },
    {
      name: 'Nii',
      school: {
        name: 'New York High',
        jobTitle: 'Lead Developer',
      },
    },
  ],
  assignments: [
    {
      title: 'Draw',
      description:
        'Draw the map of the world.',
    },
    {
      title: 'Paint',
      description:
        'Paint a statue in your neighborhood.',
    },
  ],
}
Enter fullscreen mode Exit fullscreen mode

Traversing the userInfo Object with a recursive function requires the Base case and the Recursive case.

function resursionExample(object) {
  if (typeof object === 'object') {
    for (const key in object) {
      deepIterator(object[key])
    }
  } else {
    console.log(object)
  }
}
Enter fullscreen mode Exit fullscreen mode

Base Case
The base case of a recursive function returns a value without performing a recursive call.

else {
    console.log(object)
  }
Enter fullscreen mode Exit fullscreen mode

Recursive case

function resursionExample(object) {
  if (typeof object === 'object') {
    for (const key in object) {
      deepIterator(object[key])
    }
Enter fullscreen mode Exit fullscreen mode

Recursion Pitfalls
A common pitfall when designing recursive algorithms include not defining a base case.This will lead to causing infinite recursion, and inefficient memory usage.

Recursive algorithms can be less efficient than their iterative counterparts. This is because each recursive call involves overhead, such as saving the state of the current function and setting up the new function. In contrast, iterative algorithms use a loop to repeat operations, which can be faster and use less memory.

Conclusion
Recursive function may be easier to write and reduces unnecessary function calling.
Despite these benefits Recursive functions are generally slower than non-recursive function and may require a lot of memory space to hold intermediate results on the system stacks.
The programmer must evaluate a problem thoroughly before choose to write a recursive function

References
https://developer.mozilla.org/en-US/docs/Glossary/Recursion
https://www.tutorchase.com/answers/ib/computer-science/what-are-common-pitfalls-when-designing-recursive-algorithms
https://www.archilovers.com/stories/5232/the-8-most-extraordinary-spiral-staircases-found-around-the-world.html
https://www.collegenote.net/curriculum/data-structures-and-algorithms/41/454/

Top comments (0)