Home Segments Index Top Previous Next

243: Mainline

In principle, rabbits, previous_month, and penultimate_month should work fine together. However, if you just put them as is into a program, you soon discover that, no matter how you arrange the functions, at least one function is referred to before it is defined. In the following arrangement, for example, rabbits is referred to before it is defined.

int previous_month (int n) {return rabbits (n - 1);} 
int penultimate_month (int n) {return rabbits (n - 2);} 
int rabbits (int n) { 
  if (n == 0 || n == 1) 
    return 1; 
  else return previous_month (n) + penultimate_month (n); 
} 

The C compiler cannot compile a program that includes these three functions defined in this order, because the C compiler does not know how to prepare calls to the rabbits function before the rabbits function is defined. Yet calls to the rabbits function occur in both previous_month and penultimate_month, both of which are defined before rabbits is defined.