23B. Constant Arrays

If a function only looks at the contents of an array, and does not change what is in the array, you usually indicate that by adding const to the parameter. For example, the following function firstPositive does not change what is in array A.

  // firstPositive(A,n) returns the first value
  // in A[0, ..., n-1] that is positive.  If
  // none of them is positive, then it returns 0.

  int firstPositive(const int A[], const int n)
  {
    for(int i = 0; i < n; i++)
    {
      if(A[i] > 0)
      {
        return A[i];
      }
    }
    return 0;
  }