mardi 5 avril 2016

Recursion: Reason for outputting specified text?

I have a simple c++ recursion question. Before I ask my question I think it is best to show the code that I have.

void message(int times)
{
  if (times > 0)
  {
    cout << "This is a recursive function" << endl;
    message(times - 1);
  }
  cout << "message returning with " << times << " in times << endl;
}

Let the integer variable times be set to 5. Here is the output for the function

This is a recursive function
This is a recursive function
This is a recursive function
This is a recursive function
This is a recursive function
Message called with 0 in times
Message called with 1 in times
Message called with 2 in times
Message called with 3 in times
Message called with 4 in times
Message called with 5 in times

I understand why the statement "This is a recursive function" is outputted but I don't understand why the statement "Message called with 0-5 in times" would be outputted? Thanks

Aucun commentaire:

Enregistrer un commentaire