What is the advantage of recursion over loops in C?

Status
Not open for further replies.

ramesh babu

Newbie level 5
Joined
Jan 4, 2007
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,355
what is the advantage of recursion over loops?
 

Re: c

In my opinion recursion has an advantage over looping when the we need to store a large amount of data in each cycle (that is one loop).
In looping one would need to store this data in new variables each time whereas recursion provides a more elegant way of doing so by employing stacks.

Regards
tronix
 

c

Recursion uses stack, it always slower than loops when control variables stored in registers
 

Re: c

recursion can be usefull in algorithms like backtracking and dynamic programming algorithms ...but before making use of recursion make sure tht ur program terminates after some condition fails r else it may end up in stack overflow and the system may hang up....
 

Re: c

In simple words

Recursion is efficient in terms of logic less time comsumption of programming but are more memory consuming as memory acess are usally slow so make it slow in terms of run time

loops are difficult to programme but are more efficient as we are not using one function again and again and are using counters
 

Re: c

repetitive recursion may cause stack overflow so be careful...
 

c

you have to define constant inner loops but in recursion it is dynamic.
 

c

For some applications recursive algorithm is much more simpler then corresponding looping algorithm, for example if you want to access the elements of singly linked link list in reverse order.

Also many mathematical algorithms can be implements in more natural way using recursive algorithm i.e. your implementation will look exactly same as mathematical definition of that algorithm, for example factorial and Fibonacci.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…