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 and also output 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 <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 void 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 NOT return a value using its name
volume. Consequently it is declared as void.
The function has four parameters, three of them float ( 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 first being 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 volume(1, r, h1, h2); volume(2, r, h1, h2); volume(3, r, h1, h2);
|
|
// 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.
Note that we don't need a return statement here. |
// definition of function volume void 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; cout<<" \n\n volume of cylindrical part = " << v <<" cubic meters"<< endl; } else if(choice == 2) { v = PI * radius * radius * hcon / 3.; cout<<" \n\n volume of conical part = " << v <<" cubic meters"<< endl; } else if(choice == 3) { v = PI * radius * radius * ( hcyl + hcon / 3. ); cout<<" \n\n total volume = " << v <<" cubic meters"<< endl; } else { v = 0; cout<<" You entered a non valid choice. The value zero will be returned"; } // no returns here }
|
|
/* 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