Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Why recursion?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I read about this community is for "Programming puzzles, challenges, interview questions, and similar fun".

One question is generally asked in interview is "What is the use of recursion functions, if we can write the other logic?"

As far as I know, for critical programming using recursion we can write more simplified logic than that of general functions.

Is there any reason to write recursive code?
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with recursion per se. Recursive algorithms are easier to program (and the code is easier to read) than if I were to translate it into an iterative algorithm. There are benefits in having code that's easy to read and write.

There certainly is an overhead in allocating stack frames for each recursive call, but whether that's worthwhile working around is a different question. Writing something like QuickSort iteratively would involve setting up additional data structures (a stack) as well, so that too has a certain amount of overhead.

The overhead becomes sizeable when the problem is tree-recursive (i.e., involves more than one recursive call per step) and goes to great depths. Fibonacci numbers are a good example of when using recursion is the wrong thing to do - the iterative approach has linear complexity, while the recursive approach has exponential complexity, in addition to the stack frame allocation overhead.

But generally, recursion is nothing to be feared.
 
lowercase baba
Posts: 12753
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer their question with another question: "Why would you use other logic when you can write recursive a function?"
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initially posted by Fred

Why would you use other logic when you can write recursive a function?



That is the best reply to this question that I have come across till now. Although if you are a fresher I wont advice you say that to your
interviewer or he might react like this
 
fred rosenberger
lowercase baba
Posts: 12753
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in all seriousness, the answer to this question is the same as the answer to ANY question about software development - you need more info. What is important on this system? What constraints do you have?

If memory is an issue (i.e. a phone or hand-held device application), recursion might not be very wise, especially (as Ulf says) you're going to get pretty deep.

However, the logic for recursion can be simpler, so maintenance and development might be easier. Maybe there are well known recursive algorithms that are well documented already, while you'd have to 'roll your own' non-recursive solution.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Why would you use other logic when you can write recursive a function?"



1. Recursion will be bad for code readability - not every programmer can understand it.
2. Recursion is a repeated method call stack - the more you use recursion the more memory stack created
3. Recursion can not be inlined by compilers - if i am not wrong.
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Recursion will be bad for code readability - not every programmer can understand it.


That depends on whether a straightforward non-recursive algorithm exists. Programming a recursive algorithm using recursion will certainly result in easier to read code than the same algorithm programmed in a non-recursive way.

As an example, compare recursive and non-recursive QuickSort implementations.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by mani vannan:
3. Recursion can not be inlined by compilers - if i am not wrong.



Tail recursion can be easily inlined, however it is my understanding that the Java creators specifically chose not to implement this, as it makes resultant code hard to debug (if not impossible).

- Andrew
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is there any reason to write recursive code?


Several.I am listing 2:
1) When we learn coding for mathematical functions like n! (factorial).The coding is much easier and in fact more readable.
2) XML processing : Well, these are in essence mathematical function only, but not that apparent from the surface.So, when you have to process repeating nodes in a XML document, recursion is a boon.Or so I find.
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mani vannan wrote:
1. Recursion will be bad for code readability - not every programmer can understand it.



If some [ person ] can't understand recursion then he shouldn't work as programmer.

mani vannan wrote:
2. Recursion is a repeated method call stack - the more you use recursion the more memory stack created



Tail recursion optimization can replace recursion with a loop.

mani vannan wrote:
3. Recursion can not be inlined by compilers - if i am not wrong.



Depends on compiler I think. Recursion is a paramount of functional programming.

[ UD: Please adhere to the "Be Nice" rule. ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:
The overhead becomes sizeable when the problem is tree-recursive (i.e., involves more than one recursive call per step) and goes to great depths.



I think that is what i am trying to do here. Please tell me if recursion is good for this or not. Please suggest ways to make a simple MergeSort program.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!