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 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 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.
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.
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.
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 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.
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
- Readability: Use Let when it improves code clarity.
- Consistency: Be consistent within a project.
- Modern Style: Omit Let for more concise code.
- Teaching: Use Let when teaching beginners.
- Legacy Code: Keep Let when maintaining old code.
This tutorial covered the FreeBasic Let
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.