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

why don't we talk
why don't we songs
why don't we merch
why don't we members
why don't we 8 letters
why don't we 2020
why don't we ages
why don't we tour

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?

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

Why Don't We, Why Don't We - Chills [Official Music Video] “Chills” out now: https://t.co/ 4eOQBCvNj5?amp=1 Duration: 2:46 Posted: Jan 5, 2020 Text us if you need us Merch Events. All content © Why Don't We Privacy Policy | Terms Of Service | AdChoices | Terms Of Service | AdChoices

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.

Why Don't We - Unbelievable [Official Music Video], Music videos for our songs including Chills, What Am I, Unbelievable, I Don't Belong In This Club, Cold In LA, 8 Letters, Talk, Something Different, and These� Daniel Seavey • Zach Herron • Corbyn Besson • Jonah Marais • Jack Avery

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.

Why Don't We - Chills [Official Music Video], 'These Girls' Available NOW: http://whydntwe.co/TheseGirlsYT FOLLOW US https: //twitter.com Duration: 2:50 Posted: Aug 30, 2017 4.7m Followers, 64 Following, 743 Posts - See Instagram photos and videos from Why Don’t We (@whydontwemusic)

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.

Why Don't We, Why Don't We - What Am I [Official Music Video] “What Am I” out now: https://wdw. lnk.to/WhatAmIyt Duration: 3:30 Posted: Aug 22, 2019 Why Some People Don't Wear Masks Health officials and lawmakers on both sides of the aisle are urging Americans to wear face masks in public. NPR looks into several reasons people give for not

These Girls - Why Don't We [Official Music Video], 4.7m Followers, 64 Following, 743 Posts - See Instagram photos and videos from Why Don't We (@whydontwemusic) Why Don’t We is a boyband consisting of Daniel Seavey, Zach Herron, Corbyn Besson, Jonah Marais and Jack Avery. Seavey was a contestant on season 14 of American Idol while the other four members

Why Don't We - What Am I [Official Video], The latest Tweets from Why Don't We (@whydontwemusic). @seaveydaniel • @ jackaverymusic • @corbynbesson • @jonahmarais • @imzachherron. Studio. Why Don't We & Macklemore - I Don't Belong In This Club [Official Music Video] - Duration: 3:43. Why Don't We 20,493,680 views. 3:43. The Greatest Showman | This Is Me (Official Lyric Video) -

Why Don't We (@whydontwemusic) • Instagram photos and videos, Why don't you have your stimulus check yet? It's probably one of a handful of reasons. To start, your payment may be scheduled to arrive later this year from the IRS. But the explanation could be

Comments
  • const is redundant here, what do you imagne is happening?
  • @M.M 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?
  • Since the parameter inside the function is a local copy, adding const is completely meaningless.
  • "fabsf won't change the value of the input, will it?" How could you tell? The parameter is passed by value.
  • 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.)
  • 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 prototype
  • Can const go in function definitions without going in the prototype?
  • @user24205 yes it can
  • "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".