A named constant is a value that cannot change throughout the course of the program, like, for example:
following my previous example, this code would generate an error:
That will generate an error because you cannot re-assign a value to a constant.
As it stands, writing 'centimeters = inches*2.54' will work, as long as 'inches' has already been given a value. You will have to rewrite that line every time you want to give 'centimeters' a new value, though, so what I would do is put your conversion into a function, like this:
That way, whenever you want to update the value of 'centimeter', you just need to set it equal to the function 'toCentimeter', with the length in inches (either as a numeric value, or as a variable) as your argument.
Code:
const double PI = 3.14159;
//or
const int LENGTH_OF_DAY_IN_HOURS = 24
following my previous example, this code would generate an error:
Code:
const int MY_CONSTANT_INT = 15;
//some code here.
MY_CONSTANT_INT = 7;
As it stands, writing 'centimeters = inches*2.54' will work, as long as 'inches' has already been given a value. You will have to rewrite that line every time you want to give 'centimeters' a new value, though, so what I would do is put your conversion into a function, like this:
Code:
double toCentimeter(double inches){
return inches*2.54;
}
int main(){
//some code here.
int inches = 12;
double centimeter = toCentimeter(inches);
}
Last edited: