ZetCode

FreeBasic Let Keyword

last modified June 16, 2025

The FreeBasic Let keyword is used for variable assignment. While optional in most cases, it can make code more readable by explicitly showing assignment operations. This tutorial covers all aspects of using Let.

Basic Definition

In FreeBasic, Let is an optional keyword that precedes variable assignment. It comes from older BASIC dialects where it was mandatory. FreeBasic maintains compatibility while making it optional.

The Let keyword explicitly indicates that a value is being assigned to a variable. While modern code often omits it, using Let can improve clarity in certain situations, especially for beginners.

Simple Variable Assignment

This example demonstrates basic usage of the Let keyword for assignment.

let_simple.bas
Let x = 10
Let y = 20
Let sum = x + y

Print "x: "; x
Print "y: "; y
Print "sum: "; sum

Here we use Let to assign values to three variables. The first two get literal values, while sum gets the result of an expression. The Let keyword makes each assignment operation very explicit and clear.

Let with Different Data Types

The Let keyword works with all data types in FreeBasic.

let_datatypes.bas
Let name As String = "John Doe"
Let age As Integer = 35
Let price As Double = 19.99
Let isActive As Boolean = True

Print "Name: "; name
Print "Age: "; age
Print "Price: "; price
Print "Active: "; isActive

This example shows Let with various data types. Each variable is declared and assigned in one statement. The Let keyword works consistently across all FreeBasic data types.

Let with Arrays

The Let keyword can also be used with array elements.

let_array.bas
Dim numbers(1 To 5) As Integer

For i As Integer = 1 To 5
    Let numbers(i) = i * 10
Next

For i As Integer = 1 To 5
    Print "numbers("; i; "): "; numbers(i)
Next

Here we use Let to assign values to array elements in a loop. Each array element gets a value that's ten times its index. The Let keyword makes the assignment operation stand out clearly.

Let with User-Defined Types

The Let keyword works with user-defined types (structures) as well.

let_udt.bas
Type Person
    name As String
    age As Integer
End Type

Dim p As Person
Let p.name = "Alice"
Let p.age = 28

Print "Name: "; p.name
Print "Age: "; p.age

This example demonstrates using Let with structure members. We create a Person type and then use Let to assign values to its fields. The Let keyword helps distinguish field assignments from other operations.

Let with Function Return Values

Let can be used when assigning function return values to variables.

let_function.bas
Function Square(n As Integer) As Integer
    Return n * n
End Function

Let result = Square(5)
Print "Square of 5: "; result

Here we use Let to assign the return value of the Square function to a variable. This makes it immediately clear that we're storing the function's result rather than just calling the function.

Let with Multiple Assignments

FreeBasic allows multiple assignments in one statement with Let.

let_multiple.bas
Let a = 1, b = 2, c = 3
Print "a: "; a
Print "b: "; b
Print "c: "; c

Let x = 10, y = x * 2, z = y + 5
Print "x: "; x
Print "y: "; y
Print "z: "; z

This example shows multiple assignments in single Let statements. The first assigns literal values, while the second uses expressions that reference previously assigned variables. Let makes these compound assignments clearer.

Let with Reference Variables

The Let keyword works with reference variables (@) in FreeBasic.

let_reference.bas
Dim original As Integer = 100
Dim ref As Integer Ptr = @original

Let *ref = 200

Print "Original: "; original
Print "Dereferenced: "; *ref

Here we use Let to assign a value through a pointer. The Let keyword helps distinguish the dereferenced assignment from other pointer operations. Both the original variable and dereferenced pointer show the new value.

Best Practices

This tutorial covered the FreeBasic Let keyword with practical examples showing its usage in different scenarios.

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.