FreeBasic Const Keyword
last modified June 16, 2025
The FreeBasic Const
keyword is used to declare constants -
values that cannot be modified during program execution. Constants make
code more readable and maintainable by replacing magic numbers with
meaningful names.
Basic Definition
In FreeBasic, Const
declares an identifier whose value
remains fixed throughout the program. Constants must be initialized
when declared and cannot be changed later.
Constants improve code clarity by giving meaningful names to values. They also prevent accidental modification of important values. FreeBasic supports numeric, string, and Boolean constants.
Basic Constant Declaration
This example shows the simplest form of constant declaration.
Const PI = 3.1415926535 Const MAX_USERS = 100 Const APP_NAME = "My Application" Print "PI: "; PI Print "MAX_USERS: "; MAX_USERS Print "APP_NAME: "; APP_NAME
Here we declare three constants with different data types. PI is a floating-point constant, MAX_USERS is an integer, and APP_NAME is a string. Constants follow the same naming rules as variables but cannot be reassigned.
Typed Constants
Constants can have explicit type declarations for better type safety.
Const VERSION As Single = 1.2 Const TIMEOUT As Integer = 30 Const ENABLED As Boolean = True Print "VERSION: "; VERSION Print "TIMEOUT: "; TIMEOUT Print "ENABLED: "; ENABLED
This example demonstrates typed constants. The As clause specifies the data type explicitly. Typed constants help prevent type-related errors and make the code more self-documenting.
Constant Expressions
Constants can be initialized with expressions evaluated at compile time.
Const HOURS_PER_DAY = 24 Const MINUTES_PER_HOUR = 60 Const MINUTES_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR Print "Minutes in a day: "; MINUTES_PER_DAY Const CIRCLE_DEGREES = 360 Const RIGHT_ANGLE = CIRCLE_DEGREES / 4 Print "Right angle: "; RIGHT_ANGLE
Here we use constant expressions to calculate new constants. The compiler evaluates these expressions during compilation. This allows for maintainable calculations that don't incur runtime overhead.
String Constants
String constants are useful for fixed text values used throughout a program.
Const WELCOME_MSG = "Welcome to FreeBasic!" Const PROMPT = "Enter your name: " Const ERROR_404 = "404 - Page not found" Print WELCOME_MSG Print PROMPT Input name$ Print "Hello, "; name$; "!" Print ERROR_404
This example shows string constants for user interface messages. Using constants for strings makes it easier to maintain and localize applications. Changes only need to be made in one place.
Array Constants
FreeBasic allows creating constant arrays for fixed data sets.
Const DAYS(1 To 7) As String = ("Sunday", "Monday", "Tuesday", _ "Wednesday", "Thursday", "Friday", _ "Saturday") For i As Integer = 1 To 7 Print "Day "; i; ": "; DAYS(i) Next
Here we declare a constant array of weekday names. The array size and contents are fixed at compile time. Constant arrays are useful for lookup tables and other immutable data collections.
Scope of Constants
Constants follow the same scoping rules as variables in FreeBasic.
Const GLOBAL_CONST = 42 Sub TestScope Const LOCAL_CONST = 100 Print "Local constant: "; LOCAL_CONST Print "Global constant: "; GLOBAL_CONST End Sub TestScope Print "Global constant: "; GLOBAL_CONST ' Print LOCAL_CONST ' This would cause an error
This example demonstrates constant scope. GLOBAL_CONST is accessible throughout the program, while LOCAL_CONST is only visible within the TestScope subroutine. Constants declared in procedures are local to that procedure.
Enumerated Constants
FreeBasic supports enumerated constants for related values.
Enum Colors RED = 1 GREEN = 2 BLUE = 4 End Enum Const BACKGROUND As Colors = BLUE Const FOREGROUND As Colors = GREEN Print "Background color code: "; BACKGROUND Print "Foreground color code: "; FOREGROUND
This example shows enumerated constants for color values. Enums create a set of related named constants. They improve code readability by replacing magic numbers with meaningful names.
Best Practices
- Naming: Use ALL_CAPS with underscores for constant names.
- Organization: Group related constants together.
- Documentation: Comment constants that aren't self-explanatory.
- Scope: Use the smallest possible scope for constants.
- Types: Specify types for important constants.
This tutorial covered the FreeBasic Const
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.