Good day,
I've got a question about why program 19-3 won't display a returning message but program 19-2 will.

THIS IS 19-2

// This program demonstrates a simple recursive function.
#include <iostream>
using namespace std;

// Function prototype
void message(int);

int main()
{
   message(5);
   return 0;
}

//************************************************************
// Definition of function message. If the value in times is  *
// greater than 0, the message is displayed and the function *
// is recursively called with the argument times - 1.        *
//************************************************************

void message(int times)
{
   cout << "message called with " << times << " in times.\n";

   if (times > 0)
   {
      cout << "This is a recursive function.\n";
      message(times - 1);
   }

   cout << "message returning with " << times;
   cout << " in times.\n";
}

THIS IS 19-3

// This program demonstrates a recursive function to
// calculate the factorial of a number.
#include <iostream>
using namespace std;

// Function prototype
int factorial(int);

int main()
{
   int number;

   // Get a number from the user.
   cout << "Enter an integer value and I will display\n";
   cout << "its factorial: ";
   cin >> number;

   // Display the factorial of the number.
   cout << "The factorial of " << number << " is ";
   cout << factorial(number) << endl;

   system("pause");
   return 0;
}

//*************************************************************
// Definition of factorial. A recursive function to calculate *
// the factorial of the parameter n.                          *
//*************************************************************

int factorial(int n)
{
    {
   if (n == 0)

      return 1;   
                       // Base case
   else

      return n * factorial(n - 1); // Recursive case
      }
cout << "returning: "<< n << endl;     //THIS WAS ADDED TO GET OUTPUT!!!
}

Recommended Answers

All 8 Replies

I wasn't clear about what I meant by return message. This is what returns from 19-2,i'd like a similar return from 19-3.

message called with 5 in times.
This is a recursive function.
message called with 4 in times.
This is a recursive function.
message called with 3 in times.
This is a recursive function.
message called with 2 in times.
This is a recursive function.
message called with 1 in times.
This is a recursive function.
message called with 0 in times.
message returning with 0 in times.
message returning with 1 in times.
message returning with 2 in times.
message returning with 3 in times.
message returning with 4 in times.
message returning with 5 in times.
Press any key to continue . . .

1st use the indentation. [ code ] [ /code ] blocks.

2. Use a good indentation.

oky your program should print the return message before returning.

//*************************************************************
// Definition of factorial. A recursive function to calculate *
// the factorial of the parameter n. *
//*************************************************************

int factorial(int n)
{
int _return  ;  
   if (n == 0)
       _return =1;
   else
       _return=n * factorial(n - 1); // Recursive case

   cout << "returning: "<< n << endl; //THIS WAS ADDED TO GET OUTPUT!!!
   return _return;
}

Here is a properly formatted version of your function:

int factorial(int n)
{
  {
    if (n == 0)
      return 1; 
    // Base case
    else
      return n * factorial(n - 1); // Recursive case
  }
  cout << "returning: "<< n << endl; //THIS WAS ADDED TO GET OUTPUT!!!
}

As you should be able to see, both of the possible logic paths have a return statement in them and both occur before any type of output. As a result, the function terminates before you ever get to the output statement.

In the future, please use [code] ...code tags... [/code]. Here's why:

#include <iostream>
int main() {
  std::cout << "This looks nice, and is easily readable." << std::endl;
  return 0;
}

#include <iostream>
int main() {
std::cout << "This looks like dung, and isn't readable." << std::endl;
return 0;
}

Fbody,

Thank you for your reply. I had tried coding the script the way you indicated before my initial post and got the same result, i.e. nothing returned for the cout. I've tried using the code you posted and it also did not show anything for cout.

Thank you for the tip about using code tags.

Art

The code that I posted was your code, I didn't make any changes.... I just formatted it so that you could see the issue for yourself. If you want to learn from this forum, don't just blindly cut/paste things into your code hoping they'll work.

The problem is that you have this:

function() {
  if ()
    return  //value returned, function terminated
  else
    return  //value returned, function terminated

  //nothing down here is ever reached due to previous return statement(s)
  cout << //never executes...
} //end function definition

When you need something more like this (pseudocode, do not c/p):

function() {
  if () {
    return  //value returned, function terminated
  } else {
    cout << //now it executes...
    return  //value returned, function terminated
  }
//nothing down here is ever reached due to previous return statement(s)
} //end function definition

Thanks,

So is the reason the code in 19-2 displays a message is because there is no return statement, also would there be a way to return a message for cout as the recursion was unwrapped?

Art

19-2 doesn't work because of no return statement. It works because your output occurs before the function returns (there is an implied return at the end of a void function).

Your function from 19-3 never reaches the output statement because of the explicit return statements in your if-else control structure. There is no way for the flow of control to get past the control structure because all paths through it end with a return.

Fbody,

Thanks alot for your help, it's clearer now.

Art

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.