Recursion in Programming and When to Use or Not to Use It

  • Context: Insights 
  • Thread starter Thread starter bhobba
  • Start date Start date
  • Tags Tags
    Programming Recursion
Click For Summary
SUMMARY

Recursion is a programming technique where a function calls itself to solve problems, often simplifying complex tasks. While languages like Pascal and LISP support recursion effectively, FORTRAN and COBOL present limitations. Key challenges include ensuring proper termination of recursive calls to avoid StackOverflow exceptions, particularly in languages like Java. Tail call optimization in functional languages mitigates stack overflow risks, making recursion more efficient.

PREREQUISITES
  • Understanding of recursion and its mechanics
  • Familiarity with programming languages such as Python, Java, and LISP
  • Knowledge of stack memory management and overflow issues
  • Basic concepts of functional programming and tail call optimization
NEXT STEPS
  • Learn about recursion in Python, focusing on default arguments and termination conditions
  • Explore tail call optimization in functional programming languages like LISP and Haskell
  • Study stack memory management and techniques to prevent StackOverflow exceptions
  • Implement recursive algorithms in various programming languages, including C++ and MIPS assembly
USEFUL FOR

Software developers, computer science students, and anyone interested in mastering recursion and its applications in programming.

  • #61
PeterDonis said:
so many algorithms can be implemented as loops instead of recursively
TL;DR ANY algorithm can be implemented with either loops or recursive function calls, so (unless and until you identify that the implementation is too expensive in time or memory) use whichever you find easier to code and therefore more reliable and easier to maintain.

Details
  • ANY algorithm can be implemented with loops instead of recursive function calls, however some are easier to implement recursively (e.g. quicksort). Which runs faster and/or with less memory depends on a number of factors.
  • ANY algorithm can be implemented with recursive function calls instead of loops, however some are easier to implement with loops (e.g. Fibonacci numbers). Algorithms that are easier to implement with loops run faster and/or with less memory unless the recursion can be performed with exclusively tail calls and the compiler is designed (and set) to optimize very aggressively.
  • SOME algorithms cannot be implemented with exclusively tail call recursion. These are often easier to implement recursively because iterative implementation requires the programmer to maintain one or more stacks: again quicksort is a good example, and again which (if either) runs faster and/or with less memory depends on a number of factors.
 
  • Like
Likes   Reactions: Mark44
Technology news on Phys.org
  • #62
pbuk said:
ANY algorithm can be implemented with either loops or recursive function calls
Yes, I was being imprecise; by "loop" I meant a loop that does not have to add more information to some data structure on every iteration; it can update information on every iteration, but only if it throws away the old information so the total information kept track of is of (roughly) constant size.

An example in Python would be a for loop using the range built-in iterator; this does not compute all of the numbers to be iterated over in advance, it only computes them one at a time as they are needed, and throws away the old one each time it computes a new one.

You could of course implement something similar using recursion, as long as whatever language you were using could optimize away the parts that would require storing more data on each iteration (for example, tail call optimization to eliminate repeated pushing of parameters on the stack).
 
  • #63
pbuk said:
TANY algorithm can be implemented with loops instead of recursive function calls, however some are easier to implement recursively (e.g. quicksort). Which runs faster and/or with less memory depends on a number of factors.

Yes. But it's the whole structured programming thing we had constantly drummed into us during my university days. The choice is made on which will be the most maintainable. Deciding that of course is both art and science. The last time I posted about it on another forum frequented by more modern programmers than me the response I got was - Give me that old-time religion :DD:DD:DD:DD:DD.

Thanks
Bill
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
15K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K