In this example the purpose and algorithm are given in the main function
only.
However a better approach is to give the purpose and algorithm for each
one of the functions that are included in our program. |
/* 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
ymmetrically 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 <iostream.h>
|
By inserting the prototype of the function at this point (before main)
we ensure that main will know the characteristics of function volume. |
// Prototype the function float volume ( int choice, float radius, float hcyl, float hcon );
|
Some programmers prefer to insert the entire definition of a function
before the point that it will be used. However that approach may create
confusion especially when many functions are used (typical program).
Note that this function will return a value using its name volume.
The value returned will be of the type float. The function has four
parameters, three of them floats ( radius, hcyl, hcon) and one of type
integer ( choice). We should point out that the actual names of
the parameters are unimportant (in fact, sometimes are excluded from the
prototype) what matters is the types of the parameters, their number,
and their ordering. In other words the above prototype tells our main
function that the function volume has four parameters, the firstbeing
an integer and the other three floats. Of course the programmer
of main must know that the second parameter corresponds to the radius
of the cylinder, the third to the height of the cylinder and the fourth
to the height of the cone, however he/she can choose to give arguments
to these parameters that have names of his/her choice.
|
|
// 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
|
At this point we call the function three different times. In all three
cases the values of the arguments r, h1 and h2 are the same. But the value
of the argument provided for the first parameter of the function is different. |
// 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; }
|
This is the definition of the function.
Note that the formal parameter list declares choice, radius, hcyl, and hcon.
These parameters can be used in the expressions of the definition. In addition,
we may need some local variables, for example v in this case. We have to
declare the local variables. Note that the global constant PI is also
available for our expressions. |
// 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 ); }
|
The return statement at the end indicates what will the name of the
function volume contain. As it is written now, it will contain the value
of the appropriate volume. But suppose we had written:
return( 0.5);
then no matter what the arguments were, we would end up with the value
of 0.5 being transferred to the main function.
|
|
/* 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.... */
|
© 2001 J.Y. Kazakia. All rights reserved