0

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  }
  • 1
    Better question: "this code compiled and executed well with const, is there a special reason for not using const here?". – Mankarse Sep 09 '14 at 16:14
  • @JonathanDursi: Not a duplicate, because there's no parameter of form `const T xx[]`, only `const T` asked about. – Deduplicator Sep 09 '14 at 16:38
  • @Deduplicator : I see the argument, but the answers given to that question specifically bring up issues like pointers, references, etc. There's no reason for this question to stand on its own, although there may certainly be a better dup suggestion. – Jonathan Dursi Sep 09 '14 at 16:56

5 Answers5

5

You use const to achive const-correctness. This means you've told the compiler (and any reader of your code) all the things that don't change in themselves or objects they work on. You have done this for the args to sum, correctly saying that they aren't changed in the function. The reason why you bother to do this is because it maskes the code easier to reason about (for the reader) and opens up optimisation possibilities (for the compiler).

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • And allows passing an array of `const int`. (Top-level qualifiers of function arguments are not part of the function declaration, but only the definition, see `length`. And in this case qualifiers can be added on assignment to the argument) – Deduplicator Sep 09 '14 at 16:19
2

There is a lot of purpose behind the first const, no real need for the second one.

It would compile without const but try this function:

int inc(const int array[], const int length) 
{
    for(int i = 0; i < length; ++i)
       ++array[i];
}

and your code should not compile.

Remove the first const and it will.

This shows you the purpose of the first const. Promises not to mutate the data.

Conversely, try changing the calling function:

int main() 
{
   const int arr[] = {1, 2, 3, 4, 5, 6, 7};
   cout << "Sum: " << sum(arr, 7) << endl;
   return 0;
}

Now if you try compiling sum() without the first const you will get a compiler error because you cannot convert arr to a non-const array (essentially pointer).

However even though 7 is a constant, you can still remove the second const in function sum as it is passed by value.

CashCow
  • 30,981
  • 5
  • 61
  • 92
1
int sum(const int array[], const int length) {/* ... */}

While the first const is not top-level (array is of type unqualified pointer to const int), the second one is.

Thus, the second one is relevant to the function definition, it does not change the function declaration at all, and should thus be left out of a pure declaration as extraneous noise.

Equivalent declarations:

int sum(const int* array, int length); // Removed synatactic sugar and useless const
int sum(const int* const array, const int length); // Added top-level const

Building on the first part, the reasons for the two const are completely different:

  • The reason for const-qualifying length is the exact same as for any local variable: Avoiding inadvertant changes, by directly changing it or inadvertantly passing it to a modifying function.
  • The reason for const-qualifying the element type of the pointer array (array-notation used for clarifying that it is intended to be an array), is guaranteeing to the caller that the elements wont be changed. That allows passing pointer to both const-qualified and un-qualified ints.
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

The first const promises to never change the array. For the code you've posted it doesn't make a difference. But imagine that arr had been declared in main as int const arr[] instead. The function as written would still work, but without the const it wouldn't, despite never changing the array.

The const before length however doesn't change the interface of the function. It just causes the local argument variable length to be unchangeable. This can be a safeguard if you indeed don't want to change length in the implementation (because the compiler will flag any change of length as error).

celtschk
  • 19,311
  • 3
  • 39
  • 64
0

There are two qualifiers const in the function declaration

int sum(const int array[], const int length);

In fact the second const qualifier does not have any special meaning for using the function. These two function declarations

int sum(const int array[], const int length);

int sum(const int array[], int length);

declare the same one function. This const only plays a role within the body of the function.

As for the first const qualifier then it allows to use constant arrays with this function. For example consider your code with the following array definition

const int arr[] = {1, 2, 3, 4, 5, 6, 7};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335