Recursion 


C Programming Tutorial 

Go to the Table of Contents

Vist the Gifcom Corporation

The daemon which swallowed its tail.

This section is about program structures which can talk about themselves. What happens to a function which makes a call itself? Examine the function below:

 Well_Function ()

 {
 /* ... other statements ... */

 Well_Function ();
 }

Well_Function() is said to be a recursive function. It is defined in terms of itself: it contains itself and it calls itself. It swallows its own tail! The act of self-reference is called recursion. What happens to such a function when it is called in a C program? In the simple example above, something dramatic and fatal happens. The computer, naturally, begins executing the statements in the function, inside the curly braces. This much is only normal: programs are designed to do this and the computer could do no more and no less. Eventually the program comes upon the statement Well_Function(); and it makes a call to that function again. It then begins executing statements in Well_function(), from the beginning, as though it were a new function, until it comes upon the statement Well_Function() and then it calls the function again....

This kind of function calling scenario is doomed to continue without end, as, each time the function is called, it is inevitably called again. The computer becomes totally consumed with the task of calling Well_Function() over and over. It is apparently doomed to repeat the same procedure for ever. Or is it?