mirror of
https://github.com/QB64Official/qb64.git
synced 2024-09-28 11:17:47 +00:00
71 lines
3 KiB
Text
71 lines
3 KiB
Text
The [[CONST]] statement globally defines one or more named numeric or string values which will not change while the program is running.
|
|
|
|
|
|
{{PageSyntax}}
|
|
: [[CONST]] {{Parameter|constantName}} = {{Parameter|value}}[, ...]
|
|
|
|
|
|
{{PageParameters}}
|
|
* {{Parameter|constantName}} is the constant name or list of names assigned by the programmer.
|
|
* {{Parameter|value}} is the value to initialize the global constant which cannot change once defined.
|
|
** If {{Parameter|constantName}} specifies a numeric type, {{Parameter|value}} must be a numeric expression containing literals and other constants.
|
|
** If {{Parameter|constantName}} specifies a string type, the {{Parameter|value}} must be a literal value.
|
|
|
|
|
|
{{PageDescription}}
|
|
* The {{Parameter|constantName}} does not have to include a type suffix. The datatype is automatically infered by the compiler using the {{Parameter|value}}.
|
|
* Constant values cannot reference a variable, [[SUB]] or [[FUNCTION]] return values when defined.
|
|
** The exception to the above are color functions [[_RGB32]] and [[_RGBA32]], which can be used in a CONST statement. See ''Example 2'' below.
|
|
* Constants cannot be reassigned values. They retain the same value throughout all of the program procedures.
|
|
* Constants defined in module-level code have [[SHARED|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.
|
|
|
|
|
|
{{PageExamples}}
|
|
''Example 1:'' Display the circumference and area of circles:
|
|
{{CodeStart}}' Declare a numeric constant approximately equal to the ratio of a circle's
|
|
' circumference to its diameter:
|
|
{{Cl|CONST}} PI = 3.141593
|
|
|
|
' Declare some string constants:
|
|
{{Cl|CONST}} circumferenceText = "The circumference of the circle is"
|
|
{{Cl|CONST}} areaText = "The area of the circle is"
|
|
|
|
{{Cl|DO...LOOP|DO}}
|
|
{{Cl|INPUT}} "Enter the radius of a circle or zero to quit"; radius
|
|
{{Cl|IF...THEN|IF}} radius = 0 {{Cl|IF...THEN|THEN}} {{Cl|END}}
|
|
{{Cl|PRINT}} circumferenceText; 2 * PI * radius
|
|
{{Cl|PRINT}} areaText; PI * radius * radius ' radius squared
|
|
{{Cl|PRINT}}
|
|
{{Cl|DO...LOOP|LOOP}}
|
|
{{CodeEnd}}
|
|
{{OutputStart}}Enter the radius of a circle or zero to quit? ''10''
|
|
The circumference of the circle is 62.83186
|
|
The area of the circle is 314.1593
|
|
|
|
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''
|
|
{{OutputEnd}}
|
|
: ''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.
|
|
|
|
|
|
''Example 2'': Using _RGB32 to set a constant's value.
|
|
{{CodeStart}} '' ''
|
|
{{Cl|CONST}} Red = _RGB32(255,0,0)
|
|
|
|
{{Cl|COLOR}} Red
|
|
{{Cl|PRINT}} "Hello World"
|
|
{{CodeEnd}}
|
|
|
|
{{PageSeeAlso}}
|
|
* [[DIM]], [[SHARED]]
|
|
* [[STATIC]], [[COMMON]]
|
|
* [[_PI]], [[_RGB32]], [[_RGBA32]]
|
|
* [http://doc.pcsoft.fr/en-US/?6510001 Windows 32 API constant values]
|
|
|
|
|
|
{{PageNavigation}}
|