Recursion with a Single Recursive Call
If a recursive function calls itself, then the newly called function calls itself, and so on, ad infinitum unless the code includes something to terminate the chain of calls. The usual method is to make the recursive call part of an if
statement. For example, a type void
recursive function called recurs()
can have a form like this:
void recurs(argumentlist){ statements1 if (test) recurs(arguments) statements2}
With luck or foresight, test
eventually becomes false
, and the chain of calls is broken.
Recursive calls produce an intriguing chain of events. As long as the if
statement remains true
, each call to recurs()
executes statements1
and then invokes a new incarnation of recurs() ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.