C - What is the advantage for the user const in the parameters of functions that are not pointers?

I would like to know if there is any advantage for the user when calling a function, for example

A) void func (int i);

or type function

B) void func (const int i);

As inside the func parameter, I will be copied to the stack anyway (or wherever the compiler chose in its optimizations), for the user who calls this function, there is no difference between A and B.

So, if the implementation has something like:

A) void func(int i) { i = another_func(i); printf("%d", i); } 

Or using const

 B) void func(const int i) { int j = another_func(i); printf("%d", j); } 

My question is:

Is there any advantage to implementing B? Can the compiler perform any optimization? A and B are a simple example; these questions apply to other situations.

I understand the advantage of using const in pointers (for example, const void * data), because we tell the user that we will not change its contents, but I do not understand the use of const, except for warning the programmer of a function that he should not change, but in In my opinion, using const in the header of an API file is useless.

Thank you for your help.

+6
source share
4 answers

Some people say that you should write const unless you change this variable, since you can declare any local variable const . (The main goal is to prevent the variable from being accidentally changed, but it can also help the compiler optimize your code)

Other people say that you should not declare a functional parameter const , because it will output part of the implementation in the API.

I think they are both right! This is why you can omit the declaration of the const function: you can write void func(int); in the header, but implement it void func(const int i) {} in the code file.

+4
source

There is at least one reason why not using const in non-pointer type parameters for external functions. Using const you expose the API internals, which should be irrelevant to the API user.

Using const usually helps with optimization, but its main use is to prevent errors and document the API.

+2
source

This is a warning to the programmer, yes. But this is more than just a warning. The compiler forces the constant, making sure that the programmer does not change the value unintentionally. In this, this is a way of coding the API and algorithm design information. And since the constant is provided by calls to nested functions, it is stored in the entire call chain. Note that at some point in the call chain, someone might also use a pointer to a const value.

The compiler also uses a constant for optimization. Here is a good answer .

+2
source

The const declaration also allows you or other developers working on the same code to modify it when writing your function. This is a protective programming method that makes it clear that this parameter should not be changed, maybe when you return to the code after six months, you cannot use obscene habits lazily, using the input parameter for other purposes inside your function helps prevent unpleasant errors that may arise due to unintentionally confusing == and = , etc.

0
source

Source: https://habr.com/ru/post/947400/


All Articles