const non-pointer function parameters

L

lovecreatesbea...

Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;

f(i);
return 0;
}
 
R

Richard Heathfield

(e-mail address removed) said:
Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;

f(i);

You have asked the wrong question. Here is a more important and urgent
question for you: "why is it a very bad idea indeed to pass
indeterminate values to functions?"
 
P

pete

Shoud we declare non-pointer function parameters with const keywords?
int f(const int i);

It isn't usually done that way.
A const parameter might seem to make sense
if you're viewing the definition of the the function but,
it isn't usually written that way because
the calling function is not affected by
what happens to the value of i inside of f.
 
A

Army1987

Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;
Supposing you do something else here...
f(i);
return 0;
}

When you call f(i), the value of i is copied into a local variable in f()'s
stack, which in this case is called i too.
Let's use different names for them to show the distinction:

int main(void) {
int f(const int k);
int i;
/* do something */
f(i); /*let's suppose i equals 5 here*/
return 0;
}

int f(const int k) {
/* do something */
}

As soon as f(i) is called, the local variable k in function f() is
initialized to 5. The variable k is discarded as soon as f() returns.
Anyway, the local variable i in function main() is unaffected by f() (which
is not even aware that i exists). The const in the function prototype says
that f() should never change the value of k during its execution. If *this*
is what you want, it's ok to write int f(const int k) (or int f(const int
i), or int f(const int whatever); anyway the name of the parameter won't
conflict with that of any local variables in main(), since f() can't even
see them).
 
D

David Thompson

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.
 
B

Bill Pursell

Shoud we declare non-pointer function parameters with const keywords?

Usually, I would say no. But it can be helpful. eg:

int
really_convoluted_function_I_have_to_maintain( int x )
{
/*
* Lots of stuff that covers hundreds, maybe thousands
* of lines, that I don't have time to look at:
*/

/* Finally, a reference to x. */

/* more code */
}

Facing that function, I might glance through the code and
conclude that x is not being modified by the code above the
first reference that I see to x. I can search for instances
of x in the code, but I keep seeing instances of the name
x in comments or in #defined conditionals, so I'm not
really certain what's going on. As a confirmation,
I might modify the formal parameter list and add a const
qualifier so that the compiler will either confirm or
refute my hypothesis.

In other words, the only use I see for it is to
provide information that should be more readily
available by writing clean code in the first place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top