Question

What's the problem with this recursive function?

void printNum(int n)

{

cout << n << endl;

printNum(n-1);

} // end printNum

Answers

No condition to terminate the recursion.

#1

Your homework?

"The problem" depends on what you are trying to do, which you haven't specified. But one problem is there is no termination condition, so the function will just keep on calling itself until it exhausts the available stack space, whereupon your program will crash with a segfault or similar.

Oh, and it's not indented properly either.

Oh, and you would never actually use recursion for printing out the numbers from n down to zero, or similar. You'd use iteration. This is fine if you are trying to learn about recursion, of course.

#2