468,783 Members | 1,684 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

const non-pointer function parameters

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

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

f(i);
return 0;
}

Mar 26 '07 #1
7 1895
lovecreatesbea...@gmail.com 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?"

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #2
lovecreatesbea...@gmail.com wrote:
Shoud we declare non-pointer function parameters with const keywords?
Why would you want to? What effect do you think it would have?

--
Ian Collins.
Mar 26 '07 #3
lovecreatesbea...@gmail.com wrote:
>
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.

--
pete
Mar 26 '07 #4
"lovecreatesbea...@gmail.com" <lo***************@gmail.comha scritto nel
messaggio news:11**********************@n59g2000hsh.googlegr oups.com...
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).
Mar 26 '07 #5
On 26 Mar 2007 02:26:28 -0700, "lovecreatesbea...@gmail.com"
<lo***************@gmail.comwrote:
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.

Apr 15 '07 #6
On Mar 26, 10:26 am, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
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.

--
Bill Pursell

Apr 15 '07 #7
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
Shoud we declare non-pointer function parameters with const
keywords?

int f(const int i);
Ford: Yes, if you like.
Barman: Will that help?
Ford: No.

--
Peter

Apr 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Nick Savoiu | last post: by
1 post views Thread by Dave | last post: by
2 posts views Thread by Mark Stijnman | last post: by
10 posts views Thread by S.Tobias | last post: by
1 post views Thread by nsdevelop12 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.