User Tools

Site Tools


cs509:cplusplus_function_prototypes

Example: C++ function prototypes

/*
 * Computes the area of a circle and the volume of a sphere.
 * Uses the same radius for both calculations.
 */

#include<iostream>
#include<cmath>
using namespace std;

const double PI = 3.14159;

double area( double radius );
// Returns the area of a circle with the specified radius.

double volume( double radius );
// Returns the volume of a sphere with the specified radius.

int main()
{
    double radiusOfBoth, areaOfCircle, volumeOfSphere;

    cout << "\n Enter a radius to use for both a circle and a sphere: ";
    cin >> radiusOfBoth;

    areaOfCircle = area( radiusOfBoth );
    volumeOfSphere = volume( radiusOfBoth );

    cout << "\n Radius = " << radiusOfBoth
         << "\n Area of circle = " << areaOfCircle
         << "\n Volume of sphere = " << volumeOfSphere;

    return 0;
}

double area( double radius )
{
    return ( PI * pow( radius, 2 ) );
}

double volume( double radius )
{
    return ( ( 4.0 / 3.0 ) * PI * pow( radius, 3 ) );
}
cs509/cplusplus_function_prototypes.txt · Last modified: 2013/03/06 18:56 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki