previous | start | next

## Return Values

- When the return statement is processed, the function exits immediately.

  >     double future_value(double initial_balance, double p, int n)
  >     {
  >        if (n < 0) return 0;
  >        if (p < 0) return 0;
  >        double b = initial_balance * pow(1 + p / 100, n);
  >        return b;
  >     }

- It is important that every branch of a function return a value.

  >     double future_value(double initial_balance, double p, int n)
  >     {
  >        if (p >= 0)
  >           return = initial_balance * pow(1 + p / 100, n);
  >        /* Error */
  >     }

- Functions that returns a truth value is called a predicate.

  >     bool approx_equal(double x, double y)
  >     {
  >        const double EPSILON = 1E-14;
  >        if (x == 0) return fabs(y) <= EPSILON;
  >        if (y == 0) return fabs(x) <= EPSILON;
  >        return fabs(x - y) / max(fabs(x), fabs(y)) <= EPSILON;
  >     }

previous | start | next