Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Help with recursion

Name: Anonymous 2011-05-09 20:43

So, can anyone tell me why does this http://pastie.org/1883229 recursive function just "jump out" of recursion when it returns d * 2 at depth == 1? It doesn't iterate all the way back down as it should, it just returns d * 2 to main function.

Name: Anonymous 2011-05-09 20:45

Name: Anonymous 2011-05-09 20:55

You do nothing when depth > 1.  It is going through all the stages, instead do return calc(d, depth - 1) * 2; or something.

I HELPED HIM!

Name: brunch 2011-05-09 20:56

  1 #include <stdio.h>
  2
  3 int calc(int d, int depth) {
  4         if (depth > 1) {
  5                 return calc(d, depth -1);
  6         }
  7         else if (depth == 1) {
  8                 printf("d * 2: %d\n", d * 2);
  9                 return (d * 2);
 10         }
 11 }
 12
 13
 14 int main (void) {
 15
 16         printf("Return value: %d", calc(3, 1));
 17
 18         return 0;
 19
 20 }



output:
d * 2: 6
Return value: 6

-----------------------------------------------------------

I don't know what seems to be the problem. It works fine for me.

Name: Anonymous 2011-05-09 21:59

>>3
calc(d * 2, depth - 1); bitch. TCO my anus

Name: Waka Flocka 2011-05-10 0:56

I GO HARD IN THE MOTHERFUCKING PAINT NIGGA

Don't change these.
Name: Email:
Entire Thread Thread List