**EDIT**
You questioned the use of const
. Its utility when used on parameters passed by
reference - pointers/array passed into functions - is, I think clear.
However, its use with parameters passed by value is not so clear. It plays no part in the interface seen by callers of a function, as it can have no influence on the caller. You can declare a function prototype in a public header like this
int bin_search(const int arr[], int min, int max, int element);
and then define the function implementation like this:
int bin_search(const int arr[], const int min, const int max, const int element) {...}
And the compiler will be quite happy with the difference.
So it is purely an implementation issue. Hence you should definitely not use const
on pass-by-value parameters in public prototypes, only in the implementation (if at all). Used in the implementation, const
tells the reader and the compiler
that a parameter is not (and cannot be) changed. This gives the reader some
extra information and of course the compiler will enforce this read-only
behaviour.
So shouldn't you use it on all parameters that are not changed (and some might say that parameters should never be changed)? Good question! I never use const
on call parameters but would have no hesitation in adding const
to a local variable
int cirle_area(double radius)
{
const double pi = 3.14;
return pi * radius * radius;
}
A lot of good programming style concerns consistency, so my inconsistency here
is troubling. And my previous comment - that if you make the element
parameter const
, then you should be consistent and do the same for min
and
max
uses consistency as an argument!
I'm afraid I can do no better than that. I've programmed for 20 years in C
and have rarely seen const
applied to parameters. But maybe it should be.