Handout - Side-effects


// global.cxx
//
// This program illustrates the undesirability of side-effects
//
// written by Julien Hennefeld, "Using Turbo Pascal"
// modified for C++ by Glynis Hamel
//
// This program accepts a patient's temperature and two pulse
// readings (taken five minutes apart).  It should print the temperature
// and the status of the pulse.  In the event that the patient's
// temperature is below 85 degrees, the life support system is to be
// shut off to conserve energy.  For input 98.6, 71, 74, the intended
// output is
//
// Stable pulse
// temperature 98.6
//

#include <iostream>

using namespace std;

float temp;                  // a global variable

void PulseStatus (float firstPulse, float secondPulse);

int main()
{
  float pulse1, pulse2;      // the two pulse readings

  cout << endl << "Enter temperature and two pulse rates:  ";
  cin >> temp >> pulse1 >> pulse2;

  PulseStatus(pulse1, pulse2);

  cout << "temperature " << temp << endl;
  if (temp < 85.0)
    cout << "Patient dead.  Shut off life support." << endl << endl;

  return (0);
}

#include <math.h>
void PulseStatus (float firstPulse, float secondPulse)

  // determines stability of pulse
{

  temp = firstPulse - secondPulse;      // assign difference to a temporary
                                        // variable

  if (fabs(temp) > 10.0)
    cout << "Irregular pulse" << endl;
  else
    cout << "Stable pulse" << endl;
}