# Function( const int value )

This topic is 4880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is a really generic question. Why specify const for a value that isn't a pointer or reference? Is there a reason? I'm only thinking there might be because the compiler lets me do it. When you pass something to a function, it copies it. So what's the point of it being const? Unless maybe you just didn't trust yourself to not touch a parameter's value. Just curious. Thanks for any details.

##### Share on other sites
No real reason for this. Passing as const really dosen't change anything about the parameter. I sometimes like to declare char* parameters as const char* parameters just for consistancy.

##### Share on other sites
As Stompy9999 said, there isn't really a reason.

Quote:
 Original post by Stompy9999I sometimes like to declare char* parameters as const char* parameters just for consistancy.
Actually, you should always pass as const char* where you can. E.g. this should result in a compile error:
void Log(char* sz){   printf("%s\n",sz);}Log("Hello World!"); // Error, cannot convert from const char* to char*

##### Share on other sites
Ah, that makes sense. I think a while ago I put char* and got this error and was wondering what was wrong.

##### Share on other sites
IIRC; C++ allows the 'magical' conversion of nameless temporaries from constness to non-constness. Somehow, string literals fall into this catagory. AFAIK C allows this for string literals as well; or atleast it used to.

Anyway; constness on a parameter is generally pointless unless the parameter is a reference or a pointer. But it doesn't hurt.

##### Share on other sites
Const is one of those keywords that equates to painfully removing a band-aid in one quick swoop rather then gradually pulling the band-aid off over a 10 minute period. Its up front, it gets it out of the way, you can get on with your life.

By using the const keyword along side all of the parameters, you assure yourself that if you change the value of a parameter, it'll be a compiler error, rather then a logic bug which could prove very very annoying at a later time. So, idealistically you should use it as much as you possibly can.

##### Share on other sites
 Blah...beat by Inmate2993 [lol] Type faster fingers! [/edit]
Well there's some more to it as well. Because it let's you modify a parameter, you might not always want that. For example:
int Test(const int i){   i++;}

Generates an error while
int Test(int i){   i++;}
Does not. That seems trivial now, but when you are working with a larger project, this is all to possible to sneak up on you:
 int Test(int x, int y, int z){.. Lots of code ...   for( int x = 0; x < 5; x++ )      access x - but which one?      ..... Lots of more code ...}
Since programmers go for the most convient name use, it is possible that you may accidently reuse a parameter and not even know it. If you const'd it, that would not happen.

I usually never use const. Just my own preference though, but you should hopefully be able see some of its usefullness.

- Drew

##### Share on other sites
Const is just another a way of telling the compiler "I will never change the data I give to you".I usually do not use it,but const correctness has saved a lot of people.

##### Share on other sites
Quote:
 Since programmers go for the most convient name use, it is possible that you may accidently reuse a parameter and not even know it. If you const'd it, that would not happen.

Const does not effect name hiding. In your example, wether the parameter is const or not, reffering to x inside the for loop always refers to the x declared in that for loop.

Incidentally, I always make all parameters const, and remove the const if I have to modify it.

##### Share on other sites
Quote:
 Original post by DeyjaConst does not effect name hiding. In your example, wether the parameter is const or not, reffering to x inside the for loop always refers to the x declared in that for loop.

Oh, much agreed! However, what I was hinting at is when you declare a local variable in a function and happen to have a parameter that is the same. The same goes for if you are in some class member function and declare a local variable name with the same name as a class member. Unless you explicitly tell it what to use, you are in a world of headaches when debugging.

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002135
×