Example for functions with multiple returns:

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.
 
Note that the first three arguments of the function are input while the last two are output.
/* An example on the use of functions :
               function with multiple returns       file: 5ex3.cpp
   FALL 1998
   ___________________________________
   Jacob Y. Kazakia   jyk0
   October 5, 1998
   Example 3 of week 5
   Recitation Instructor: J.Y.Kazakia
   Recitation Section  01
   ___________________________________
 
Purpose: This program uses a function  named properties to calculate the volume and total surface of a right prizm which has as base a n-sided regular polygon inscribed in a circle of a given radius.
 
The function properties has the following parameters:
a)  The radius  of the base  ( in meters )
b)  The height   ( in meters )
c)  An integer m denoting the number of sides of the regular polygon
d)  The volume
e)  The area denoting the total surface area of the solid.
 
The first 3 arguments are inputs, the last two are ouputs.
 
Algorithm:  The length of each side of the regular polygon is calculated by:
 
side = 2 * r * sin( pi / m )     Why?
 
The area of the base is calculated by :
 
base_area = pi * r * r * { sin(2*pi/m) / (2*pi/m) }   why?
 
               The volume is:
 
               volume = base_area * height
 
               The total surface area is:
 
               area = 2 * base_area + m * side * height
 
 
 
                */
By inserting the prototype of the function at this point (before main) we ensure that main will know the characteristics of function properties.
 #include <iostream.h>
 #include <math.h>
 
  // Prototype the function
 
void properties(float radius, float height, int m,
                float& volume, float& area);

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 properties.  Consequently it is declared as void.  The function has five parameters, two of them float ( radius & height) which are passed by value,one of type intm ), which is also passed by value, and two of type float& , which are passed by name.  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 five parameters, as described above.  Of course the programmer of main must know that the first parameter corresponds to the radius , the second to the height , the third to the number of sides, the fourth to the volume and the fifth to the total surface area.   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 denoting the number of sides
float r ;  // the radius
float h ; // the height of the object
 
float volume, area ;
 
//  "hard code"  the input  variables
 
r  = 2.34 ; // meters
h  = 1.12 ; // meters
m  = 9 ;
At this point we call the function .  Note that the calling statement does not differentiate between the "passed by value" and "passed by name" arguments.
// calculate volume and area ( the output variables)
by invoking the function
 
properties( r, h, m, volume, area );
 
// output the variables
 
cout<<"\n\n  For the given geometry:  radius = "<< r <<" height = "<< h;
cout<<" both in meters" << endl;
cout<<"  and for a "<< m <<"-sided solid we have volume = ";
cout << volume << " cubic meters" << endl;
cout<<"                  and total surface area = ";
cout << area << " square 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 we don't need a return statement here.
 //  definition of function volume
 
void properties(float radius, float height, int m,
                float& volume, float& area)
{
// declare the local variables
 
float pi = 3.141592654;
float side, base_area;
 
side = 2 * radius * sin(pi/m);
 
base_area = pi * radius * radius * (sin(2*pi/m)/(2*pi/m));
 
area = 2 * base_area + m * side * height;
 
volume = base_area * height;
 
// no returns here
}
 
/*      HERE IS THE OUTPUT
 
 
 
For the given geometry:  radius = 2.34  height = 1.12 both in meters
and for a 9-sided solid we have volume = 17.739 cubic meters
                and total surface area = 47.8114 square meters
 
 
enter e (exit) to terminate the program....
 
 
*/

© 2001 J.Y. Kazakia. All rights reserved