CONST: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
The [[CONST]] statement globally defines one or more named numeric or string values which will not change while the program is running.
The '''CONST''' statement globally defines one or more named numeric or string values which will not change while the program is running.




Line 24: Line 24:


{{PageExamples}}
{{PageExamples}}
''Example 1:'' Display the circumference and area of circles:
;Example 1: Display the circumference and the area of circles:
{{CodeStart}}' Declare a numeric constant approximately equal to the ratio of a circle's
{{CodeStart}}
' circumference to its diameter:
{{Text|<nowiki>' Declare a numeric constant approximately equal to the ratio of a circle's</nowiki>|#919191}}
{{Cl|CONST}} PI = 3.141593
{{Text|<nowiki>' circumference to its diameter:</nowiki>|#919191}}
{{Cl|CONST}} PI = {{Text|3.141593|#F580B1}}


' Declare some string constants:
{{Text|<nowiki>' Declare some string constants:</nowiki>|#919191}}
{{Cl|CONST}} circumferenceText = "The circumference of the circle is"
{{Cl|CONST}} circumferenceText = {{Text|<nowiki>"The circumference of the circle is"</nowiki>|#FFB100}}
{{Cl|CONST}} areaText = "The area of the circle is"
{{Cl|CONST}} areaText = {{Text|<nowiki>"The area of the circle is"</nowiki>|#FFB100}}


{{Cl|DO...LOOP|DO}}
{{Cl|DO}}
     {{Cl|INPUT}} "Enter the radius of a circle or zero to quit"; radius
     {{Cl|INPUT}} {{Text|<nowiki>"Enter the radius of a circle or zero to quit"</nowiki>|#FFB100}}; radius
     {{Cl|IF...THEN|IF}} radius = 0 {{Cl|IF...THEN|THEN}} {{Cl|END}}
     {{Cl|IF}} radius = {{Text|0|#F580B1}} {{Cl|THEN}} {{Cl|END}}
     {{Cl|PRINT}} circumferenceText; 2 * PI * radius
     {{Cl|PRINT}} circumferenceText; {{Text|2|#F580B1}} * PI * radius
     {{Cl|PRINT}} areaText; PI * radius * radius ' radius squared
     {{Cl|PRINT}} areaText; PI * radius * radius {{Text|<nowiki>' radius squared</nowiki>|#919191}}
     {{Cl|PRINT}}
     {{Cl|PRINT}}
{{Cl|DO...LOOP|LOOP}}
{{Cl|LOOP}}
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Enter the radius of a circle or zero to quit? ''10''
{{OutputStart}}
Enter the radius of a circle or zero to quit? ''10''
The circumference of the circle is 62.83186
The circumference of the circle is 62.83186
The area of the circle is 314.1593
The area of the circle is 314.1593
Line 47: Line 50:
Enter the radius of a circle or zero to quit? ''123.456''
Enter the radius of a circle or zero to quit? ''123.456''
The circumference of the circle is 775.697
The circumference of the circle is 775.697
The area of the circle is 47882.23
The area of the circle is 47882.226


Enter the radius of a circle or zero to quit? ''0''
Enter the radius of a circle or zero to quit? ''0''
{{OutputEnd}}
{{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.
{{PreStart}}
'''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.
{{PreStart}}


----


''Example 2'': Using _RGB32 to set a constant's value.
;Example 2: Using _RGB32 to set a constant's value.
{{CodeStart}}
{{CodeStart}}
{{Cl|CONST}} Red = _RGB32(255,0,0)
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}({{Text|640|#F580B1}}, {{Text|400|#F580B1}}, {{Text|32|#F580B1}})
 
{{Cl|CONST}} Red = {{Cl|_RGB32}}({{Text|255|#F580B1}}, {{Text|0|#F580B1}}, {{Text|0|#F580B1}})


{{Cl|COLOR}} Red
{{Cl|COLOR}} Red
{{Cl|PRINT}} "Hello World"
{{Cl|PRINT}} {{Text|<nowiki>"Hello World"</nowiki>|#FFB100}}
 
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{Ot|Hello World|red}}
{{OutputEnd}}





Revision as of 14:45, 8 December 2024

The CONST statement globally defines one or more named numeric or string values which will not change while the program is running.


Syntax

CONST constantName = value[, ...]


Parameters

  • 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.


Description

  • 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 or user FUNCTION return value when defined. However, other (already known) constants and most of the internal char, math or color functions can be used, such as CHR$, EXP or _RGBA32.
  • 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.
  • Since QB64-PE v4.0.0 a huge list of preset Constants is available in every program.


Examples

Example 1
Display the circumference and the area of circles:
' Declare a numeric constant approximately equal to the ratio of a circle's
' circumference to its diameter:
CONST PI = 3.141593

' Declare some string constants:
CONST circumferenceText = "The circumference of the circle is"
CONST areaText = "The area of the circle is"

DO
    INPUT "Enter the radius of a circle or zero to quit"; radius
    IF radius = 0 THEN END
    PRINT circumferenceText; 2 * PI * radius
    PRINT areaText; PI * radius * radius ' radius squared
    PRINT
LOOP
END
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.226

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.


;Example 2: Using _RGB32 to set a constant's value.
SCREEN _NEWIMAGE(640, 400, 32)

CONST Red = _RGB32(255, 0, 0)

COLOR Red
PRINT "Hello World"

END
Hello World


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link