Lilás
Lilás

Reputation: 1201

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

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

A) void func(int i);

or a function like

B) void func(const int i);

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

So if a implement something like :

A)

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

Or using the const

B)

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

My Question :

There is any advantage of implementation B ? The compiler can perform any kind of optimization ? A and B are just a simple example, these questions are valid for other situations.

I understand the advantage of using const in pointers (e.g const void *data), because we are telling the user that we are not going to modify its contents, but I do not understand the usage of const other than warn the programmer of the function that he is not supposed to modify it, but in my point of view using const in a API header file is useless.

Thanks for your help.

Upvotes: 9

Views: 3594

Answers (4)

asaelr
asaelr

Reputation: 5456

Some people here say that you should write const if you won't change the variable - as you can declare any local variable const. (The main goal is to keep you from accidently changing the variable, but also may help the compiler to optimize your code)

Other people say that you shouldn't declare function parameter const, because it will expose some of the implementation in the API.

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

Upvotes: 7

Ziffusion
Ziffusion

Reputation: 8923

It is a warning to the programmer, yes. But it is more than just a warning though. The compiler enforces const-ness by making sure that the programmer does not modify the value inadvertently. In that, it's a way to encode API and algorithm design information. And since the const-ness is assured across nested function calls, it is preserved across the entire call chain. Note that at some point in the call chain, someone may use a pointer to the const value too.

The compiler also uses the const-ness for optimizations. Here's a good answer.

Upvotes: 3

Crowman
Crowman

Reputation: 25908

Declaring it const also prevents you, or other developers working on that same code, from modifying it when you're writing your function. It's a defensive programming technique that makes it clear that that parameter is not supposed to be modified, perhaps when you return to the code six months later, prevents you from employing arguably bad habits by lazily using an input parameter for other purposes within your function, helps prevent nasty errors which can arise from inadvertently confusing == and =, and so on.

Upvotes: 1

ouah
ouah

Reputation: 145829

There is at least one reason why not to use const in non-pointer type parameters for external functions. With const your are exposing internals of the API which should be irrelevant for the API user.

The use of const can generally helps for optimizations but its main usage is to prevent errors and document the API.

Upvotes: 2

Related Questions