0

I am writing a recursive function that has two functions, one to add numbers from 0 to 10 and then the other to retrieve the first function return value and subtract it until it reaches 0. Although, my code only adds them up 10 for the calls. Can someone shed some light. thanks.

#include <iostream>
#include <fstream>
using namespace std;

static int recurse(int count)
{

   cout << count << "\n";

   if (count < 10)
   {
      recurse(count + 1);
   }

   int aRet = count;
   return count;
}

static int minusRecusive(int minus)
{

   recurse(1);
   cout << "\n\t" << minus;
   int a =0;
   minus = recurse(a);

   if (minus < 1)
   {
      recurse(minus - 1);
   }

   return minus;
}


int main()
{
   minusRecusive(1);
   cin.get();
}
4
  • 2
    What is your actual question about this? Commented Jan 27, 2012 at 15:26
  • Where are you adding the numbers from 0 to 10 ? Your only addition is the call with an incremented counter! Commented Jan 27, 2012 at 15:28
  • 1
    you first function recurse is nothing but int recurse (int count){return count;} , is that what you wanted? Commented Jan 27, 2012 at 15:31
  • 1
    The minusRecursive should probably all itself rather than the other function. Commented Jan 27, 2012 at 15:33

1 Answer 1

1

Your recurse functions doesn't actually return the sum. If you call recurse(0) it will recurse into it 10x, but your return value would still be 0. Also, you're creating aRet but it's never used. Try the following...

if (count < 10) return count + recurse(count + 1);
return count;

Your minusRecursive function should be similar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.