What Is Recursion In Programming?

Recursive code calls itself as a way of repeating tasks.

Expert Answers

An illustration of the letter 'A' in a speech bubbles

Many tasks in computer programs need to be repeated. Normally, the code used for this is iterative, instructing the computer to perform a series of operations multiple times until certain conditions are met. Recursive programming uses a slightly different strategy to repeat tasks. Rather than simply repeating steps multiple times, recursive code calls itself. Typically, one will code recursion in the form of a subroutine that will continue calling itself until certain conditions are met. One of the most important concerns in recursive programming is preventing excessive recursion as it will cause various system stacks to overflow, something not an issue in more traditional iterative programming where infinite loops can be a problem, but one does not have issues concerning the way an operating system stores return addresses from subroutines.

Because of the issues of stack overflows, when a problem can be solved iteratively, that is usually preferable. There are certain types of problems where recursion can be more efficient. In systems with limited amounts of memory, such as embedded systems, the compact nature of recursive code can save valuable space and loading time, although it will increase the size of call stacks. Also, for problems with many possible branches and options, it can be more efficient than iterative programming.

Approved by eNotes Editorial Team

Posted on

Soaring plane image

We’ll help your grades soar

Start your 48-hour free trial and unlock all the summaries, Q&A, and analyses you need to get better grades now.

  • 30,000+ book summaries
  • 20% study tools discount
  • Ad-free content
  • PDF downloads
  • 300,000+ answers
  • 5-star customer support
Start your 48-Hour Free Trial