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!!!
}