Shoud we declare non-pointer function parameters with const keywords?
int main(void){
int f(const int i);
int i;
f(i);
return 0;
}
First, it is usually a bad idea to declare functions 'locally' (within
a function). It is legal, but rare, so most other (e.g. maintenance)
programmers won't be used to it and will easily misread these
declarations or miss them when looking something up. And it is never
necessary except in the unusual and rare case that you are writing a
'private' (internal linkage) wrapper or proxy for an external routine
(or even datum).
Second, it doesn't matter to the compiler. Putting 'top level' const
on a parameter _in the definition_ does make it unwritable (legally)
within the body, and may or may not be a good idea depending on your
design and coding rules for function bodies. Putting it in the
(separate) declaration has no effect on calls and it is guaranteed to
work either way; e.g.:
/* foo.c */
int foo ( const int zorg ) { ... do stuff ... return n; }
/* bar1.c */
int foo ( const int zorg ) ; /* perhaps from header */
... foo (3) ...
/* bar2.c */
int foo ( /*unqual*/ int zorg ) ; /* ditto */
... foo (42) ...
both declarations are compatible with the definition and are
guaranteed to work the same, and indeed are compatible with each other
and can both appear (or be #include'd) in the same source and scope.
(Although I have seen a compiler, IIRC AIX xlc, get this wrong.)
Given that you do use top-level const in the definition:
The (style) disadvantage of putting it in the separate declaration(s)
used by the caller(s) is that it is unnecessary and may be confusing
to those not familiar with it, or at least add a little clutter.
The (style) advantage of putting it there is that you can make the
declaration (often in a .h file) an exact textual copy of the
beginning of the definition, either by just copy-and-paste or more
automatic and systematic means.