Home Segments Index Top Previous Next

103: Mainline

Some functions do not return values used in other computations. Instead, they are executed for some other purpose, such as displaying a value.

Accordingly, C++ allows you to use the void symbol as though it were a data type for return values. When C++ sees void used as though it were a return value data type, C++ knows that nothing is to be returned.

For example, in the following variation on the program in Segment 98, display is handled in the display_box_car_volume function, so there is no value to be returned. Accordingly, void appears instead of a data-type name in the definition of display_box_car_volume, and display_box_car_volume contains no return statement:

#include 
// Define display_box_car_volume first:                          
void display_box_car_volume (int h, int w, int l) {              
  cout << "The volume of the box car is " << h * w * l << endl;  
}                                                                
// Then, define main: 
main ( ) { 
  int height = 11, width = 9, length = 40; 
  display_box_car_volume (height, width, length); 
} 
--- Result --- 
The volume of the box car is 3960