ZetCode

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_basic.bas
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_numeric.bas
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.

var_function.bas
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.

var_udt.bas
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

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.