Thread: Can someone explain why this simple recursion function crashes.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Can someone explain why this simple recursion function crashes.

    Code:
    # include <stdio.h>
    
    // if num = 5 print 4*2=8
    // if num = 6 print 6*4*2 = 48
    
    int even_prod(int num)
    {
        if (num%2 != 0)
            
            return (even_prod(num-1));
        else
            return (num *(even_prod(num-2)));
    }
    
    int main()
    {
        printf("%d\n", even_prod(5));
    
        return 0;
    }
    it crashes but it should print 8 to the screen. Its under my nose but I don't know why.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Try using a debugger or adding some useful debugging output to see what's going on:
    Code:
    int even_prod(int num)
    {
        printf("num = %d\n", num);
        if (num%2 != 0)
            return (even_prod(num-1));
        else
            return (num *(even_prod(num-2)));
    }
    That should give you a huge clue.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You never return to main. In all cases you call the function recursively and eventually you run out of stack space.

    A recursive function has a general form of
    Code:
    foo( )
    {
       if( base-case )
         return something/nothing
       else
         recursive-call
    }
    Last edited by Tclausex; 02-15-2013 at 01:53 PM. Reason: code gernerality

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Indeed, it is recursive on all control paths. I've actually seen a compiler warning about that once.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    1
    Add this condition to your program:

    Code:
    int even_prod(int num)
    
    {
        if(num!=0)
        {
          if (num%2 != 0)
             return (even_prod(num-1));
         else
             return (num *(even_prod(num-2)));
         }
    }
    
    int main()
    {
        printf("%d\n", even_prod(5));
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by eramit2010 View Post
    Add this condition to your program:
    Close, but not quite:
    Code:
    $ cat prod.c
    #include <stdio.h>
    int even_prod(int num)
    {
        if(num!=0)
        {
            if (num%2 != 0)
                return (even_prod(num-1));
            else
                return (num *(even_prod(num-2)));
        }
    }
    
    
    int main()
    {
        printf("%d\n", even_prod(5));
    
    
        return 0;
    }
    
    
    $ make prod
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  prod.c   -o prod
    prod.c: In function ‘even_prod’:
    prod.c:11: warning: control reaches end of non-void function
    You need to return something in the case that num is zero. Since it's a product function, the multiplicative identity (1) is a good choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does my simple program crashes?
    By rock3t in forum C Programming
    Replies: 1
    Last Post: 07-02-2012, 08:01 AM
  2. need help with function recursion (simple)
    By abdullahkiran in forum C Programming
    Replies: 2
    Last Post: 05-29-2012, 01:54 AM
  3. Simple C Program that sometimes crashes!
    By logitechz in forum C Programming
    Replies: 4
    Last Post: 12-03-2011, 04:08 PM
  4. Recursion Function explain...
    By Siaw Ys in forum C Programming
    Replies: 9
    Last Post: 11-19-2011, 10:52 PM