/* An example on the use of functions : function with a single return file: 5ex1.cpp FALL 1998 ___________________________________ Jacob Y. Kazakia jyk0 October 4, 1998 Example 1 of week 5 Recitation Instructor: J.Y.Kazakia Recitation Section 01 ___________________________________ Purpose: This program uses a function named volume to calculate the various partial volumes of a composite solid body. The body consists of a cylinder of radius R and height hcyl and a cone of height hcon symmetrically sitting on top of the cylinder and having a base of the same radius as the cylinder. The function has the following arguments: a) The radius of the base ( in meters ) b) The height hcyl ( in meters ) c) The height hcon ( in meters ) d) an integer choice which takes the values 1, 2, 3 for 1, the function returns the volume of the cylindrical part only. for 2, the function returns the volume of the conical part only. for 3, the function returns the total volume. Algorithm: The volume of the cylinder is calculated by vcyl = PI * radius^2 * hcyl The volume of the cone is calculated by vcon = (1/3 ) PI * radius^2 * hcon Here PI is a constant set equal to 3.14159265358979 */ #include // Prototype the function float volume ( int choice, float radius, float hcyl, float hcon ); // Define the constant pi const float PI = 3.14159265358979 ; void main() { // declare the variables of the main function int m ; // integer to be used for choosing mode float r ; // the radius float h1 ; // the height of the cylinder float h2 ; // the height of the cone float v_cyl, v_con, v_total ; // the volumes // "hard code" the variables r = 2.34 ; // meters h1 = 1.12 ; // meters h2 = 3.12 ; // meters // calculate volumes by invoking the function v_cyl = volume(1, r, h1, h2); v_con = volume(2, r, h1, h2); v_total = volume(3, r, h1, h2); // and output volumes cout<<" \n\n volume of cylindrical part = " << v_cyl <<" cubic meters"<< endl; cout<<" \n\n volume of conical part = " << v_con <<" cubic meters"<< endl; cout<<" \n\n total volume = " << v_total <<" cubic meters"<< endl; // hold the screen cout<<" \n\n enter e (exit) to terminate the program...."; char hold; cin>>hold; } // definition of function volume float volume( int choice, float radius, float hcyl, float hcon) { // declare the local variables float v; // variable used to store locally the calculated volume if ( choice == 1) v = PI * radius * radius * hcyl; else if(choice == 2) v = PI * radius * radius * hcon / 3.; else if(choice == 3) v = PI * radius * radius * ( hcyl + hcon / 3. ); else { v = 0; cout<<" You entered a non valid choice. The value zero will be returned"; } return ( v ); } /* HERE IS THE OUTPUT volume of cylindrical part = 19.2664 cubic meters volume of conical part = 17.8902 cubic meters total volume = 37.1565 cubic meters enter e (exit) to terminate the program.... */