Author |
Message |
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.

Age:37 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Thu Jul 15, 2004 3:20 am Post maybe stupid Post subject: |
 |
|
|
|
Recursive methods/functions are good in some ways, and bad in others.
But that there is a recursive function. _________________ Performance is often the art of cheating carefully. - James Gosling |
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Thu Jul 15, 2004 3:26 am Post maybe stupid Post subject: |
 |
|
|
|
Good example for recursive functions would be factorials. Quick examples:
int fact (int num) {
if (num > 1)
return fact(num - 1) * num;
return 1;
} |
Or you could just make it into a loop, which will run a lot faster, and save more memory.
int fact (int num) {
int val = 1;
for ( ; num > 1; num--)
val *= num;
return val;
} |
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Thu Jul 15, 2004 3:32 am Post maybe stupid Post subject: |
 |
|
|
|
Recursiveness is a good way to explain/model certain things. but hardly ever a good way to implement them. As with the example above, use loops whenever possible, even if it's tougher to code. _________________ 4,691 irradiated haggis! |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Jul 15, 2004 10:34 am Post maybe stupid Post subject: |
 |
|
|
|
all recursive functions can be rewritten "iteratively" (not recursively) using a stack. And it'll probably save memory.
It is a lot easier to write recursive functions for things like trees or linked lists, once you know how to write them. You just have to be careful you're not doing the same calculations over and over again... like the fibinacii numbers example:
int fib(int num)
{
if (num > 2)
{
return fib(num - 1) + fib(num - 2);
}
return 1;
} |
This is a terrible use of recursion... as every call results in 2 more... which result in 2 more... once you call the function with num around 50-60, the result takes FOREVER.
The idea when writing recursive functions if you have 2 cases... a base case and a recursive case. The base case is the simplest case possible where the answer is known (for the fib example it's when rv <= 1. Then there's a recursive case which has to somehow make the problem approach the base case(in the fib example if the return fib(num - 1) + fib(num - 2); ). If the problem doesn't get simpler you have an infinate loop, although the program might quit because of too many function calls inside one another, maybe just on debug mode. just imagine what would happen if instead of return fib(num - 1) + fib(num - 2); it was just return fib(num);
Here's an example where recursion does help make the code simpler, deleteing the nodes of a binary tree:
void deleteTreeNode(Node *node)
{
if (node)
{
deleteTreeNode(node->left);
deleteTreeNode(node->right);
delete node;
//node = 0; if we passed the pointer by reference
}
} |
All you have to do to delete a tree, or any section of the tree is call this function with the root of your tree/subtree.
now the base case is where node == 0, or NULL. If this is the case do nothing. The recursive case is where node != 0, in which case we delete the children, and then the node itself.
Last edited by Bak on Thu Jul 15, 2004 7:56 pm, edited 2 times in total |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Jul 15, 2004 10:56 am Post maybe stupid Post subject: |
 |
|
|
|
Recursive functions can overload the stack after too many recursions.
The only case I've ever used a function calling itself for is when some initial conditions aren't met, and then it sets them and calls itself again. Those initial conditions involve database queries, so it's easier to understand (and more efficient) just calling the query over again. Because the function is only ever called once more, it might not technicly be a recursive call.
Any other REAL programming task (not mathematics) can be probably be done better with a loop. _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Thu Jul 15, 2004 1:49 pm Post maybe stupid Post subject: |
 |
|
|
|
Dr Brain wrote: | The only case I've ever used a function calling itself for is when some initial conditions aren't met, and then it sets them and calls itself again. Those initial conditions involve database queries, so it's easier to understand (and more efficient) just calling the query over again. Because the function is only ever called once more, it might not technicly be a recursive call. |
This is actually one of the few times where it would probably be better to use a goto. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Thu Jul 15, 2004 1:51 pm Post maybe stupid Post subject: |
 |
|
|
|
Wow, we all sort of agree on this subject. Lock this thread before Qndre posts something! |
|
Back to top |
|
 |
Qn... err Anonymous Guest
Offline
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Thu Jul 15, 2004 3:16 pm Post maybe stupid Post subject: |
 |
|
|
|
rofl |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Jul 15, 2004 4:01 pm Post maybe stupid Post subject: |
 |
|
|
|
^.^
Very nicely done. |
|
Back to top |
|
 |
-Smong- Guest
Offline
|
Posted: Thu Jul 15, 2004 5:56 pm Post maybe stupid Post subject: |
 |
|
|
|
It looks to me like the factoral returns 1 or 0. I've no idea about the fibonacci one, I guess if you need those functions you know all the mathematical stuff behind it. Also shows it's a good idea to comment your code. |
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Thu Jul 15, 2004 7:31 pm Post maybe stupid Post subject: |
 |
|
|
|
-Smong- wrote: | It looks to me like the factoral returns 1 or 0. |
Ever notice a function on calculators that are just a ! only? What it does is multiply every number upto the one you entered together.
So 5! would be 5 * 4 * 3 * 2 * 1. This takes a lot of calculations to do when you get to high numbers, as the numbers become large pretty fast, so saving a bit of time at each step helps.
Factorials are also a good way to test how fast your calculator is to another. Most max out with 69!, since most calculators only go upto 10^100 - 1.
For the hell of it, 69! = 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000, or 1.71122452 × 10^98 |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Jul 15, 2004 7:34 pm Post maybe stupid Post subject: |
 |
|
|
|
Factorial is defined to be the product of all the numbers from one up to the number the factorial is of. So 1! (that's one factorial) will be 1.
2! = 1 * 2 = 2
3! = 1 *2 * 3 = 6
4! = 1 * 2 * 3 * 4 = 24
the fibinacii numbers are like this: the first two number in the sequence are 1 and 1. Every number after that is the sum of the previous two numbers. so the 3rd number will be the sum of the previous two... 1 + 1 = 2. The 4th number = 1 + 2 = 3. The pattern continues...
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...
the significance is that lots of occurences in nature use numbers in this sequence, like the amount of petals on a flower will usually be one of these numbers. |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Jul 15, 2004 10:28 pm Post maybe stupid Post subject: |
 |
|
|
|
Also populations of bee hives will reflect fibonacci numbers for each generation (under ideal conditions, of course). |
|
Back to top |
|
 |
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.

Age:37 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
Posted: Fri Jul 16, 2004 1:26 am Post maybe stupid Post subject: |
 |
|
|
|
Hmm'k, thanks for replying all.
His reply to you: "it's not a serious app! write the damn loop for me then!", hehe.  |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Fri Jul 16, 2004 2:24 am Post maybe stupid Post subject: |
 |
|
|
|
same with the # of leaves on plants and hundreds of other things |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Fri Jul 16, 2004 11:53 am Post maybe stupid Post subject: |
 |
|
|
|
yeah... you should use recursive functions if you 1. don't care about efficiency (bad programming) or 2. it's on a timed comp sci test and they only give you half a page to write on. |
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
|
Back to top |
|
 |
|