.TH recursion "" "" "Definition"
.PC
.PP
.I Recursion
is the technique by which a program or function calls itself.
Because under C, all variables in a function function have local scope;
therefore, when a function calls itself, it in effect recreates itself
but with a fresh copy of each of its variables.
The ``states'' of the previous call or calls to that function are stored
on the stack, and are not modified when the function calls itself.
.PP
Recursion is a useful way to loop through a complex procedure.
Be careful, however, that you do not lose track of how the number of times
you have called a given function; and be careful not to pile the stack too
high, or you may have problems.
.SH Example
The following program demonstrates recursion.
In it, the function
.B recur()
calls itself ten times.
.DM
#include <stdio.h>
.DE
.DM
main()
{
	printf("Before recursion ... \en");
	recur(1);
	printf("After recursion ... \en");
}
.DE
.DM
recur(level)
int level;
{
	printf("Entering call to recur() number %d\en", level);
.DE
.DM
	if (level < 10)
		recur(level+1);
.DE
.DM
	printf("Leaving call to recur() number %d\en", level);
}
.DE
.SH "See Also"
.Xr "programming COHERENT" programmi
