Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is const unnecessary in function declarations in header files for parameters passed by value?

I was recently reading about the usage of const keyword as function arguments in C and the way to use that has been mentioned in When and for what purposes should the const keyword be used in C for variables and been accepted as correct answer. In this post, one point mentions that

Never use const in a function prototype for a parameter passed by value. It has no meaning and is hence just 'noise'.

I used this way and it works for me but I am not sure why that is a noise for parameters passed by value and yet not a noise for the parameters passed by reference (more aptly the pointer values in C as there is not concept of pass by value and pass by reference in C). So, by this explanation when I pass a pointer as a function argument and use a const keyword; I have to do this for both the declaration in the header file and the definition in the C file but I need not use the const keyword for a non-pointer argument in the declaration (header file) and only use it while defining the function in the C file.

Any explanations?

like image 871
Swapnil Avatar asked Jul 29 '16 13:07

Swapnil


People also ask

Should function parameters be const?

Always use const on function parameters passed by reference or pointer when their contents (what they point to) are intended NOT to be changed. This way, it becomes obvious when a variable passed by reference or pointer IS expected to be changed, because it will lack const .

What effect does the const keyword have on a function parameter?

Declare function parameters that are pointers to values not changed by the function as const. Declaring function parameters const indicates that the function promises not to change these values. In C, function arguments are passed by value rather than by reference.

Why do we use const at the end of a function header?

Putting const after a function declaration makes the function constant, meaning it cannot alter anything in the object that contains the function.

Is const qualified in the function declaration?

const values in declarations do not affect the signature of a function, so you should not be put in the function declaration. For best practice, the value should pass by reference.


2 Answers

The statement you quote is a bit misleading, because in C, all arguments are passed by value.* I suppose it is trying to distinguish between the arguments themselves and, for the special case of arguments that are pointers, their referents.

In any event, the point is that const-qualifying a function parameter in the function declaration conveys no information whatever to callers. Regardless of such qualification, the function cannot modify the caller's copy of any argument anyway, because arguments are passed by value.

*Note, however, that arrays are never passed at all. In function call expressions, as in most contexts, array values "decay" to pointers, and those pointers are passed by value. This produces an effect similar, but not identical, to what you would have if arrays were passed by reference.

like image 138
John Bollinger Avatar answered Sep 19 '22 00:09

John Bollinger


It's the rule. If in the declaration of a function, you don't mark your parameters const, you can mark them const in the definition.

Some folk like to mark as many parameters const as possible in the definition since it can guard against unintentional modification of the function parameters; which could introduce bugs. Personally I don't do this but plenty of houses (including a large bank headquartered in Scotland) insist on the style.

like image 32
Bathsheba Avatar answered Sep 19 '22 00:09

Bathsheba