Thread: Problem with recursion

  1. #1
    Registered User SKS's Avatar
    Join Date
    May 2015
    Posts
    5

    Problem with recursion

    Hi, I've just started learning C for a few weeks.
    Everything was pretty good, now I've come across a problem with recursions. This is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int factorial(int number);
    
    
    int main()
    {
        int answer = factorial(4);
        printf("%d", answer);
        return 0;
    }
    
    
    int factorial(int number)
    {
        return (number * factorial(number-1));
    }
    I don't know what the problem is. It keeps crashing when I run.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You do not have a condition that will stop the recursion at a given point, so it will keep on running indefinitely until it crashes.

    Recursion in C - Cprogramming.com

  3. #3
    Registered User SKS's Avatar
    Join Date
    May 2015
    Posts
    5
    Quote Originally Posted by Matticus View Post
    You do not have a condition that will stop the recursion at a given point, so it will keep on running indefinitely until it crashes.

    Recursion in C - Cprogramming.com
    Thank you so much! I feel stupid. I edited my factorial to this and it worked.
    Code:
    int factorial(int number)
    {
        if (number != 1)
        {
            return (number * factorial(number-1));
        }
    }

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Let's examine your program. You have sent 4 into factorial.

    4 -> 4*factorial(3)
    3 -> 3*factorial(2)
    2 -> 2*factorial(1)
    1 -> 1*factorial(0)
    0 -> 0*factorial(-1)
    -1 -> -1*factorial(-2)

    and goes on. You never get a return value from it because it opens a new function forever. Try to solve this problem and let me know if you have any further problem.

    Edit: Opps I was late

  5. #5
    Registered User SKS's Avatar
    Join Date
    May 2015
    Posts
    5
    Thank you, I've managed to figure it out.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    No need to feel stupid, you took the hint and fixed it up nice and quickly.

    You should compile with maximum warnings:

    Code:
    /*
    main.c|23|warning: control reaches end of non-void function
    */
    If a function is supposed to return a value, but does NOT return a value (which is what happens if the "if()" condition in your function is false), this could result in undefined behavior (if the calling function attempts to receive that returned value).

    An easy fix would be to put an "else" in there and return an explicit value for that situation. Since your function is using multiplication, returning 1 should work fine.

  7. #7
    Registered User SKS's Avatar
    Join Date
    May 2015
    Posts
    5
    Quote Originally Posted by Matticus View Post
    No need to feel stupid, you took the hint and fixed it up nice and quickly.

    You should compile with maximum warnings:

    Code:
    /*
    main.c|23|warning: control reaches end of non-void function
    */
    If a function is supposed to return a value, but does NOT return a value (which is what happens if the "if()" condition in your function is false), this could result in undefined behavior (if the calling function attempts to receive that returned value).

    An easy fix would be to put an "else" in there and return an explicit value for that situation. Since your function is using multiplication, returning 1 should work fine.
    Thank you for replying again. I took your suggestion and edited the function to this:
    Code:
    int factorial(int number)
    {
        if (number > 1)
            return (number * factorial(number-1));
        else return 1;
    }
    Now I can pass any value within integer range into the function without any errors (i.e 1 or 0 or -1,-2...etc).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion problem
    By alphasil in forum C Programming
    Replies: 33
    Last Post: 07-15-2012, 08:47 AM
  2. Recursion problem
    By ramayana in forum C Programming
    Replies: 3
    Last Post: 11-13-2006, 11:54 PM
  3. Recursion problem
    By mag_chan in forum C Programming
    Replies: 3
    Last Post: 11-25-2005, 03:57 AM
  4. A Problem with recursion
    By Adamkromm in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2005, 08:35 PM
  5. recursion problem
    By ender in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 02:25 PM