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.
source share