Why don't the absolute value functions in C accept const inputs?

C uses pass by value. The value for the parameter of a function is a copy of the argument you give.

It is OK to copy both const and non-const floats, and the result is a non-const float.

It is similar to assignment:

const float f = 5.5f;
float g = f;   // OK

In fact, the language specifies that the value of an expression can never be const, i.e. when a value is read from a variable, that value is not const even if the variable was.


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);

Because the C language uses pass by value semantics, any argument that you pass to it, while it could be modified internally, doesn't directly affect the value you pass in.

This means that from the caller's perspective, float fabsf( float ); and float fabsf( const float ); are the same. So there's no point in making the parameter const.

Where it does make sense to use const is if the parameter you pass in is a pointer, for example:

void print_string(char *str)

This function, despite what the name suggests, can dereference the given pointer and modify what it points, i.e. str[0] = 'x', to result in a change viewable by the calling function. If this function were defined like this:

void print_string(const char *str)

The caller is ensured that function can't perform any modifications to what str points to.