Handout - Function with input arguments


// Program to calculate the area of a triangle 

#include < iostream>
#include < cmath>

using namespace std;

float AreaTriangle (float side1, float side2, float side3);  // prototype 


int main ()
{
  float a, b, c; 	// the three sides of the triangle
  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(a, b, c);
  cout << endl << "The area of the triangle is " <<  area << endl;

  return 0;
}


/* 
 * PRE:  side1, side2, and side3 are positive numbers that
 *       form the sides of a triangle
 * POST: returns the area of a triangle with sides side1, 
 *       side2, side3
 */

float AreaTriangle (float side1, float side2, float side3)
{
  float s;		// local variable - the semiperimiter

  s = (side1 + side2 + side3) / 2.0;
  return (sqrt ( s * (s - side1) * (s - side2) * (s - side3) ) );
}