this code compiled and executed well without using const. Is there a special reason for using const in here?
0 #include <iostream>
1 using namespace std;
2
3 int sum(const int array[], const int length) {
4 long sum = 0;
5 for(int i = 0; i < length; sum += array[i++]);
6 return sum;
7 }
8
9 int main() {
10 int arr[] = {1, 2, 3, 4, 5, 6, 7};
11 cout << "Sum: " << sum(arr, 7) << endl;
12 return 0;
13 }
- code reference: MIT C++ Open Course Lecture Note 4 "Arrays and Strings"
- URL: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-096-introduction-to-c-january-iap-2011/lecture-notes/MIT6_096IAP11_lec04.pdf