A lot of math functions have this form:
float cos(const float fRadians)
I get float and fRadians, but what does the ‘const’ mean here?
A lot of math functions have this form:
float cos(const float fRadians)
I get float and fRadians, but what does the ‘const’ mean here?
The short answer is that it is a constant, it can’t be changed. I can’t find the documentation on it though.
It means the function won’t change the value passed.
int times2 (int x){x=x*2; return x;}. c=3; times2(c); // c now has the value of 6
int times2 (const int x){x=x*2; return x;}. c=3; times2(c); // c still has the value of 3
Its really there for people passing in the parameters using pointers.
Thanks for the help Foster. Not sure what “passing in the parameters using pointers” means, but I guess I can ignore it for most purposes with math functions
int times2 (int x){x=x*2; return x;}. c=3; times2(c); // c now has the value of 6
int times2 (const int x){x=x*2; return x;}. c=3; times2(c); // c still has the value of 3
@Foster In both of these cases c will still have 3, neither one of the functions affects c because it is passed on the stack by value, not by reference.
@mmarquis The const variable modifier is used in a function call as a promise that the function will not change the value of what is passed. This is really more useful for the writers of the function in that the compiler will throw a warning if anyone tries to edit/write code within the function that tries to modify the passed variable.