CONST - mkilgore/QB64pe GitHub Wiki
The CONST statement globally defines one or more named numeric or string values which will not change while the program is running.
- CONST constantName = value[,]
- constantName is the constant name or list of names assigned by the programmer.
-
value is the value to initialize the global constant which cannot change once defined.
- If constantName specifies a numeric type, value must be a numeric expression containing literals and other constants.
- If constantName specifies a string type, the value must be a literal value.
- The constantName does not have to include a type suffix. The datatype is automatically infered by the compiler using the value.
- Constant values cannot reference a variable, SUB or FUNCTION return values when defined.
- Constants cannot be reassigned values. They retain the same value throughout all of the program procedures.
- Constants defined in module-level code have shared scope, so they can also be used in SUB or FUNCTION procedures.
- Constants defined in SUB or FUNCTION procedures are local to those procedures.
- CLEAR will not affect or change constant values.
Example 1: Display the circumference and area of circles:
' Declare some string constants: CONST circumferenceText = "The circumference of the circle is" CONST areaText = "The area of the circle is" DO...LOOP INPUT "Enter the radius of a circle or zero to quit"; radius IF...THEN radius = 0 IF...THEN END PRINT circumferenceText; 2 * PI * radius PRINT areaText; PI * radius * radius ' radius squared PRINT DO...LOOP |
Enter the radius of a circle or zero to quit? ''123.456'' The circumference of the circle is 775.697 The area of the circle is 47882.23 Enter the radius of a circle or zero to quit? ''0'' |
- Explanation: PI cannot change as it is a mathematical constant so it is fitting to define it as a constant. Trying to change PI will result in a calculation error.
COLOR Red PRINT "Hello World" |
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page