[prev] 72 [next]

Arrays and Functions (cont)

Since functions do not know how large an array is:
  • pass in the size of the array as an extra parameter, or
  • include a "termination value" to mark the end of the array
So, the previous example would be more likely done as:

int total, vec[20];
…
total = sum(vec,20);

Also, since the function doesn't know the array size, it can't check whether we've written an invalid subscript (e.g. in the above example 100 or 20).