Edit
As M.M commented, on parameters in prototypes the const
is ignored. The edited source of the original answer (see below) shows this:
float correct(float const value);
float erroneous(float const value);
float changer(float value);
float correct(float value) {
return -value;
}
float erroneous(float value) {
value = -value;
return value;
}
float changer(float value) {
value = -value;
return value;
}
There is no error message.
Anyway, I'll leave the original in place in the hope it might help.
Original
The const
at a parameter makes this parameter read-only inside the function.
For example:
float correct(float const value) {
return -value;
}
float erroneous(float const value) {
value = -value;
return value;
}
float changer(float value) {
value = -value;
return value;
}
This source will not compile without error message.
The function correct()
will read the given value, change its sign, and return the negated value.
The function erroneous()
seems to do effectively the same, except that there is an assignment to the parameter. But as the parameter is const
this is not allowed.
Next, the function changer()
will work as the both before, but it gives no errors.
Let's look at the call site:
float f = 3.14159;
float g = correct(f); // or erroneous(f) or changer(f)
The variable f
given as an argument will be copied into the parameter value
. It will never change even if changer()
will be called.
You might like to look at parameters as some kind of local variables. Actually they are mostly handled like this in the generated machine code.
So, why do you see const
sometimes? You see it if a pointer is defined as parameter.
When you don't want the value pointed to to be changed, you need to add const
; but do it at the correct position!
void effective(int const * pointer);
void futile(int * const pointer);
void possible_but_overly_restricted(int const * const pointer);
const
is redundant here, what do you imagne is happening? – Decompensationconst
is completely meaningless. – Empathyfloat const x = -1.0; float y = fabsf(x);
so it seems to me thatfabsf
does accept const inputs. There's no way to say "you can pass me afloat
by value but you can't pass aconst float
." (And as we see in the answers, C does not provide a way to require that the input to a function be afloat const
.) – Kling