Home Segments Index Top Previous Next

135: Mainline

Suppose that you want to write many functions that involve, say, the interest rate offered to you by your bank on deposits and your marginal tax rate. You could wire the current rates into such functions, but then you would have to edit and recompile your program whenever rates change. A more efficient alternative is to have your program pick up values for those rates from your keyboard whenever your program runs.

The following program, for example, obtains values for the two rates and assigns those values to global variables, deposit_rate and marginal_rate, at run time. Those global variables are then used to compute the net rate:

#include  
/* Define deposit_rate and marginal_rate to be global variables */ 
double deposit_rate, marginal_rate; 
void display_net_rate ( ) { 
  printf ("The net rate for deposits in your bracket is %f percent.\n",  
          deposit_rate * (1 - marginal_rate / 100.0)); 
} 
/* Then, define main */ 
main ( ) { 
  scanf ("%lf%lf", &deposit_rate, &marginal_rate); 
  display_net_rate ( ); 
} 
--- Data ---
3.5 38.5 
--- Result --- 
The net rate for deposits in your bracket is 2.152500 percent.