previous | start | next

## Vector Parameters and Return Values (Vector Parameters)

- Functions and procedures often have vector parameters.

  >     double average(vector<double> v)
  >     {
  >        if (v.size() == 0) return 0;
  >        double sum = 0;
  >        for (int i = 0; i < v.size(); i++)
  >           sum = sum + v[i];
  >        return sum / v.size();
  >     }

- A vector can be passed by value or by reference.
- Pass by reference is used for modifying individual elements of the vector.

  >     void raise_by_percent(vector<double>& v, double p)
  >     {
  >        for (int i = 0; i < v.size(); i++)
  >           v[i] =v[i] * (1 + p / 100);
  >     }

previous | start | next