Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 20th, 2005, 7:37 PM   #1
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Recursion

The books where I learned programming from were almost all opposed to using recursion, but my computer science teacher tells us to use recursion religiously. What's your opinion on using recursion? Should recursion be used, or is it better to be avoided?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jan 20th, 2005, 9:04 PM   #2
Tama
Programmer
 
Join Date: Dec 2004
Posts: 35
Rep Power: 0 Tama is on a distinguished road
Recursion is best used where it solves the problem with considerably less effort and without being terribly inefficient. Anyone who has a strong opinion either way is needlessly restricting himself.
Tama is offline   Reply With Quote
Old Jan 20th, 2005, 9:24 PM   #3
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Both recursion and iteration have their upsides and downsides. Recursion is less efficient but more versatile (and sometimes the only way to write an algorithm). Iteration on the other hand is simpler (in my opinion), sometimes faster, and usually more efficient (especially in terms of stack usage), but is much more limited in ability and scope. I generally prefer iteration, but like Tama said, don't restrict yourself by leaning to strongly one way or the other.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Old Jan 21st, 2005, 3:30 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Go for both. I despised it, until I learnt about binary trees.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 21st, 2005, 3:33 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
Its far harder to debug a recursive algorithm, and they are more likly to lead to memory leaks and infinate loops/recursions
Berto is offline   Reply With Quote
Old Jan 21st, 2005, 9:38 AM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Well, I need not saw what side of the fence I am on (if you aren't sure why, check my alias). Also, recursion leads to efficiency, which leads to robustness... but some things need not be recursive... if you think the code is going into iterative overkill, you can probably swap the algorithm out with a more recursive oriented one.

btw: Infinite Recursion and missing that damn semicolon at the end of my c++ programs was very common when I was in college... and not just common for me, but the whole department... its the "nature of the beast".
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 21st, 2005, 4:58 PM   #7
Lethns
Newbie
 
Join Date: Nov 2004
Posts: 22
Rep Power: 0 Lethns is on a distinguished road
Quote:
Originally Posted by Ooble
Go for both. I despised it, until I learnt about binary trees.
Same here
Lethns is offline   Reply With Quote
Old Jan 26th, 2005, 8:29 AM   #8
drewlander
Newbie
 
drewlander's Avatar
 
Join Date: Jan 2005
Location: Pittsburgh, PA
Posts: 16
Rep Power: 0 drewlander is on a distinguished road
Send a message via AIM to drewlander
Go for whatever gets your problem solved the best.
__________________
On and on....
drewlander is offline   Reply With Quote
Old Jan 26th, 2005, 9:00 AM   #9
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
There are some cases where a recursive algorithm isn't more efficient, but then you get to fields like Dynamic Programming etc, of which i don't know too much. Say you need to compute x primes using a recursive algorithm. Every time you goto compute the next one, you have to start from the beginning, so you're recalculating everything x times. Dynamic programming tries to eliminate that by storing calculated values when you calculate them the first time. There's my 2 cents..

Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 16th, 2005, 9:50 PM   #10
xaosai
Newbie
 
xaosai's Avatar
 
Join Date: Mar 2005
Posts: 8
Rep Power: 0 xaosai is on a distinguished road
Some languages are optimized for it. I write a lot of Lisp and I really dont use iteration that much because recursion works so well. I've found that you can usually solve a problem either way, but depending on the algorithm and the code one may be faster/more efficient than the other.
xaosai is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC