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

4

23

In C, the prototype for the absolute value function (that accepts a float) is

 float fabsf( float );

Why doesn't this prototype accept a constant value, like this:

 float fabsf( float const );

fabsf won't change the value of the input, will it?

If I have a function that accepts an input and calls fabsf, am I forced to avoid specifying the input as const?

What is the appropriate way to handle const correctness in this situation?

Wrangler answered 13/12, 2019 at 4:47 Comment(5)
const is redundant here, what do you imagne is happening?Decompensation
@Decompensation I expect it would create a compile time error if I tried to change the value of the input inside the function. Is this incorrect?Wrangler
Since the parameter inside the function is a local copy, adding const is completely meaningless.Empathy
"fabsf won't change the value of the input, will it?" How could you tell? The parameter is passed by value.Golf
The following code is legal C: float const x = -1.0; float y = fabsf(x); so it seems to me that fabsf does accept const inputs. There's no way to say "you can pass me a float by value but you can't pass a const float." (And as we see in the answers, C does not provide a way to require that the input to a function be a float const.)Kling
H
14

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);
Homoousian answered 13/12, 2019 at 7:47 Comment(3)
The question is about prototypes though, the prototype float fabsf( float const ); has nothing to do with the function implementation (which does not have to repeat the const), in fact the const is ignored entirely in the prototypeDecompensation
Can const go in function definitions without going in the prototype?Wrangler
@Wrangler yes it canEnceladus
D
34

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.

Decompensation answered 13/12, 2019 at 4:52 Comment(0)
H
14

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);
Homoousian answered 13/12, 2019 at 7:47 Comment(3)
The question is about prototypes though, the prototype float fabsf( float const ); has nothing to do with the function implementation (which does not have to repeat the const), in fact the const is ignored entirely in the prototypeDecompensation
Can const go in function definitions without going in the prototype?Wrangler
@Wrangler yes it canEnceladus
S
8

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.

Switcheroo answered 13/12, 2019 at 13:59 Comment(1)
"The caller is ensured that function can't perform any modifications ..." is not true. The function knows the address of the data and can therefore modify it, with, e.g.: ((char*)str)[0] = 'f'. The const ... * in the arguments list is therefore only a "declaration of intent".Chivalrous
E
5

To add a language lawyer perspective:

For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types. [..] In the determination of type compatibility and of a composite type, [..] each parameter declared with qualified type is taken as having the unqualified version of its declared type.

N1570 6.7.6.3/15

That means these two are compatible:

void foo(int const);
void foo(int);

Therefore you can write the prototype with or without const (which means without makes more sense; less to type / read) and can add const in the function definition if you want to avoid accidentally modifying the (copied - call by value!) parameter inside the functions body.

Enceladus answered 13/12, 2019 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.