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.