Home Segments Index Top Previous Next

128: Mainline

In the following program, doubling_time is redefined, yet again, albeit awkwardly, to illustrate the limited availability of parameter values:

#include  
/* Define doubling_time_aux and doubling_time first */ 
double doubling_time_aux ( ) { 
  /* Following line DEFECTIVE! */ 
  return log (2.0) / log (1.0 + r); 
} 
double doubling_time (double r) { 
  r = r / 100.0; 
  return doubling_time_aux ( ); 
} 
/* Then, define main */ 
main ( ) { 
  printf ("The doubling time is %f.\n", doubling_time (5.0)); 
  /* Following line DEFECTIVE! */ 
  printf ("The value r is %f.\n", r); 
} 

In this program, the doubling_time function asks a subfunction, doubling_time_aux—a function defined with no parameters—to perform the actual computation. The C compiler cannot compile doubling_time_aux, however, because no value for r is available to doubling_time_aux.

Moreover, C cannot compile the second printf statement in the main function. The reason is that r exists only during the execution of the function in which it appears as a parameter; r no longer exists once that function has returned.