#include <iostream>
usingnamespace std;
int sum( int* xs, unsigned n )
{
int result = 0;
while (n--)
{
result += xs[ n ];
}
return result;
}
int main()
{
int numbers[] = { 7, 8, 9, 10, 11 };
cout << "The sum is " << sum( numbers, 5 ) << ".\n"; // prints 45
return 0;
}
Hope this helps.
[edit] If you want to be specific about it, you can prototype your function arguments to indicate that the argument array will not be modified:
First, why do you want to pass an array by value? It can be done in other ways. You can make a struct that contains the array and pass the struct instance by value. Or you can use the std::vector instead of a c-array. Still, I am not sure why you would want to pass the array by value.