Home Segments Index Top Previous Next

134: Mainline

Suppose that you want to perform certain computations involving the mathematical constant pi. You could use 3.14159 explicitly in such places, but a more elegant alternative is to define a global variable, pi, with the appropriate value:

#include  
// First define pi to be a global variable: 
double pi = 3.14159; 
// Then, define tank_car_volume, a function that uses pi: 
double tank_car_volume (double r, double l) { 
  return pi * r * r * l; 
} 
// Then, define main: 
main ( ) { 
  cout << "The volume of a standard tank car is "  
       << tank_car_volume (3.5, 40.0) << endl; 
} 
--- Result --- 
The volume of a standard tank car is 1539.38