Usually, it is common belief that there is two kinds of numerical values in computer programming, variables and constants.
I am confused at what a "constant" is, and I was most confused by the "const" keyword in C++ for insance. I can count many types of "constants" :
1) Universal constants like pi, e, etc... Those never changes and are the only true constants.
2) Compile-time constants. You decide their values when you compile your program, and never change this.
3) Mathematical constant. They are almost the same as compile time constant, except they are not defined directly but are the result of another calculation involving several constants of the first two mentioned kinds. Like if you insert (3*10 / 4) in your code.
4) Constant argument to function. It's when you call a function and one of your arguments is one of the already mentionned 3 types of constants. Like in "my_function(2);"
5) Local constants. They are affected at their creation (can also be an argument inside a function, which is a special case of a variable/constant being created then affected by the caller). They can be affected by variables making them take different values at execution time. However they are "constant" in the sense that you can't affect any value to them a second time.
As far I know there is 2 reasons to have constants in the 1st place :
1) Security - you don't want to accidentally affect a value to a "variable" whenre this shouldn't be allowed
2) Optimisation - knowing some value is constant allows the code to compile into something faster and smaller (for instance, if you know you multiply by 4, you avoid a MUL instruction and just preform a shift left by 2).
All those 5 types of "constants" can lead to various optimisations which are drastically different from each others :
1) In a HLL, since a constant is universal you could build it in the language directly, and avoid having the user define his own PIs and Es again and again. However this won't make any difference in compiled code
2) Any compile time constant does not have to be stored in variable RAM, it can just be hardcoded with the ROM or program RAM.
3) Any math involving more than one compile-time constant should be done by the compiler and not by the program ! Only exception : When it affects precision in integer math, as in 4*var/3 will not be the same as (4/3)*var = 1 * var !
4) This can optimise the way in which the function is called since there is no need to push a variable on the argument stack, but just a raw number
5) In the case of a function call with a "constant" argument, you could pass the argument by reference even if the user doesn't specify this, as it will never be modified anyway
In the case of a user declared "constant", it doesn't make much of a difference I think.
Apparently C used the const keyword for 5, but it can also mean 1, 2 and 3 but those have to be detected by the compiler in order to be optimized.
I am confused at what a "constant" is, and I was most confused by the "const" keyword in C++ for insance. I can count many types of "constants" :
1) Universal constants like pi, e, etc... Those never changes and are the only true constants.
2) Compile-time constants. You decide their values when you compile your program, and never change this.
3) Mathematical constant. They are almost the same as compile time constant, except they are not defined directly but are the result of another calculation involving several constants of the first two mentioned kinds. Like if you insert (3*10 / 4) in your code.
4) Constant argument to function. It's when you call a function and one of your arguments is one of the already mentionned 3 types of constants. Like in "my_function(2);"
5) Local constants. They are affected at their creation (can also be an argument inside a function, which is a special case of a variable/constant being created then affected by the caller). They can be affected by variables making them take different values at execution time. However they are "constant" in the sense that you can't affect any value to them a second time.
As far I know there is 2 reasons to have constants in the 1st place :
1) Security - you don't want to accidentally affect a value to a "variable" whenre this shouldn't be allowed
2) Optimisation - knowing some value is constant allows the code to compile into something faster and smaller (for instance, if you know you multiply by 4, you avoid a MUL instruction and just preform a shift left by 2).
All those 5 types of "constants" can lead to various optimisations which are drastically different from each others :
1) In a HLL, since a constant is universal you could build it in the language directly, and avoid having the user define his own PIs and Es again and again. However this won't make any difference in compiled code
2) Any compile time constant does not have to be stored in variable RAM, it can just be hardcoded with the ROM or program RAM.
3) Any math involving more than one compile-time constant should be done by the compiler and not by the program ! Only exception : When it affects precision in integer math, as in 4*var/3 will not be the same as (4/3)*var = 1 * var !
4) This can optimise the way in which the function is called since there is no need to push a variable on the argument stack, but just a raw number
5) In the case of a function call with a "constant" argument, you could pass the argument by reference even if the user doesn't specify this, as it will never be modified anyway
In the case of a user declared "constant", it doesn't make much of a difference I think.
Apparently C used the const keyword for 5, but it can also mean 1, 2 and 3 but those have to be detected by the compiler in order to be optimized.