Handout - Function using global variables
// Program to calculate the area of a triangle
// USE OF GLOBAL VARIABLES IS, UNDER MOST CIRCUMSTANCES,
// CONSIDERED POOR PROGRAMMING PRACTICE.
//
#include < iostream>
#include < cmath>
using namespace std;
float AreaTriangle (); // prototype
// DECLARE GLOBAL VARIABLES
float a, b, c; // the three sides of the triangle
int main ()
{
float area;
cout << endl << "This program calculates the area of a triangle";
cout << endl << " given the lengths of the three sides" << endl;
cout << "Enter lengths of the three sides: ";
cin >> a >> b >> c;
area = AreaTriangle();
cout << endl << "The area of the triangle is " << area << endl;
return 0;
}
/*
* PRE: a, b, and c (WHAT ARE THEY?) are positive numbers that
* form the sides of a triangle
* POST: returns the area of a triangle with sides a, b, c
*/
float AreaTriangle ()
{
float s; // local variable - the semiperimiter
s = (a + b + c) / 2.0;
return (sqrt ( s * (s - a) * (s - b) * (s - c) ) );
}