FreeBasic Var Keyword
last modified June 23, 2025
The FreeBasic Var
keyword allows for type-inferred variable
declaration. It automatically determines the variable type from the
initializer expression. This makes code more concise while maintaining
type safety.
Basic Definition
In FreeBasic, Var
is a keyword used for declaring variables
with implicit typing. The compiler infers the variable type from the
initialization expression at compile time.
Variables declared with Var
are still strongly typed. The type
is fixed after declaration. This differs from dynamic typing where types
can change at runtime.
Basic Var Declaration
This example shows the simplest usage of the Var
keyword.
Var number = 42 Var message = "Hello, FreeBasic!" Print "number is: "; number Print "message is: "; message
Here we declare two variables using Var
. The compiler infers
number
as Integer and message
as String.
Var with Numeric Types
Var
works with all numeric types in FreeBasic.
Var intValue = 100 Var floatValue = 3.14159 Var doubleValue = 2.718281828459045 Print "intValue: "; intValue Print "floatValue: "; floatValue Print "doubleValue: "; doubleValue
This demonstrates Var
with different numeric literals.
Integer
literals produce Integer
types, while
floating-point literals produce Single
or Double
depending on precision.
Var in Function Declarations
Var
can be used for local variables within functions.
Function CalculateArea(radius As Double) As Double Var pi = 3.141592653589793 Var area = pi * radius * radius Return area End Function Var circleArea = CalculateArea(5.0) Print "Area of circle: "; circleArea
Inside the CalculateArea
function, we use Var
for both
constants and intermediate calculations. The types are correctly inferred as
Double
to match the function parameters and return type.
Var with User-Defined Types
Var
works with user-defined types (UDTs) in FreeBasic.
Type Person firstName As String lastName As String occupation As String Declare Constructor(firstName As String, lastName As String, occupation As String) End Type Constructor Person (firstName As String, lastName As String, occupation As String) This.firstName = firstName This.lastName = lastName This.occupation = occupation End Constructor Var p = Person("John", "Doe", "gardener") Print "First name: "; p.firstName; ", Last name: "; p.lastName; ", Occupation: "; p.occupation
This example shows Var
with a user-defined type. The compiler
correctly infers the type as Person
from the constructor call.
The syntax becomes cleaner while maintaining type safety.
This tutorial covered the FreeBasic Var
keyword with practical
examples showing its usage in different scenarios. Var
makes
code more concise while maintaining FreeBasic's strong typing.
Author
List all FreeBasic Tutorials.