954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recursive Function Problem

I have a problem with a program I'm writing. It's supposed to find the factorial of the number you input, but it's not. The problem seems to be that the recursive function isn't...well...recursing. Right now, when compiled, the program multiplies the number by the number before it. So if it's 5 that you put in, it gives you 20, instead of 120, which it would if the factorial thing worked.

Don't tell me the whole answer to the problem; it would be nice if you could just point me in the right direction.

Oh, and please remember that I'm a novice. I've only been coding lightly for about 3 or 4 months, perhaps.

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

And I guess it would help if I put the code in here...

#include <iostream>
using namespace std;
void recurse(int input) //the recurse function...
{
     if (input == 0){ //if the input becomes 0, exit
               return;
               }
     recurse(input - 1); //calls itself
}
int main()
{
    int input;
    
    cout<<"Input a number to find the factorial of:\n";
    cin>>input; //get input value
    cin.get();
    
    recurse (input); //calls recurse function
    cout<<input * (input-1)<<"\n"; //multiplies it all together
    cin.get();
}
orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

Two problems:
1) need a return value to return the factorial. Your function does the recursion but doesn't calculate anything.

2) The function's return value should be int, not void.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

You're not modifying the variable, just taking it as a parameter.
Do you have to use recursion and not a loop?

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

cout<

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

I think the problem is that I don't know how to take the output of a function and use it...

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

If a function returns, say, an int, you can use a call to that function in every place you could have used an int.
Examples:

#include <iostream>

int sumFunction(int a, int b) {
     return a + b;
}

int main() {
     int a = 5, b = 7, c = 0;
     c = sumFunction(a, b); // c now is 5 + 7 = 12
     b = a + sumFunction(a, c); // b now is 5 + (5 + 12) = 22;
     std:cout << "123 + 123 = " << sumFunction(123, 123) << std::endl;
     return EXIT_SUCCESS;
}


So if you want to evaluate n!, your function should return that number for you to do whatever you like with it.
The purpose on making it recursive is that you know a base case (0! = 1 by definition) and you have n! = (n-1)! * n (a recursive definition of the factorial).

Combine the two things and remember to output the result.

int factorial(int n) {
     if(n==0) {
          return 1; // 0! = 1
     }
     return /* I leave it up to you what to return here. Hint: it should contain a call to factorial(int n) itself (hence recursion) */
}


Hope this helps.

mrboolf
Junior Poster
183 posts since Jun 2008
Reputation Points: 134
Solved Threads: 18
 
I think the problem is that I don't know how to take the output of a function and use it...
int iReturn = recurse (input);

or 

int iReturn = 0;

iReturn = recurse (input);


Also, your factorial calculation should be done inside the recursive function.

Liinker
Newbie Poster
15 posts since Dec 2008
Reputation Points: 13
Solved Threads: 3
 

I finally got this one! YES!!! Thanks everyone, I'm so ecstatic now. This thread is finally solved, I finally understand recursion, etc. etc. Hooray! And I was just beating myself up five minutes ago because I couldn't get it. Finally...yes...I feel so good about myself now. Thanks everyone for your help. Here's the completed program:

#include <iostream>
using namespace std;

int factorial(int x);

int main(){
    int x;
    
    cout<<"Input a number to find the factorial of:\n";
    cin >> x;
    cin.ignore();
    
    cout<<"The factorial of " << x << " is " << factorial(x) << "\n";
    cin.get();
}
int factorial(int x){ 
    if (x == 0){
          return 1;
          }
    return x * factorial(x - 1); //it took me MONTHS to get THIS?!?!
}
orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

New problem though...(i guess it's not really a problem, just something weird) When i put in 20 to find the factorial, I get -2102132736...now I know it's not a negative number. Is this just an inherent bug in the program?

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You