ZetCode

FreeBasic Name Keyword

last modified June 16, 2025

The FreeBasic Name keyword allows creating aliases for existing variables, functions, or types. It provides a way to refer to the same entity with different names in your code.

Basic Definition

In FreeBasic, Name is a keyword used to create alternative names for program elements. It doesn't create new objects but provides additional references to existing ones.

The Name keyword is useful for improving code readability, providing backward compatibility, or creating shorter names for frequently used elements. It works with variables, functions, and types.

Aliasing a Variable

This example shows how to create an alias for an existing variable.

name_variable.bas
Dim original As Integer = 42
Name alias As original

alias = 100
Print "original: "; original
Print "alias: "; alias

Here we create a variable original and then make alias refer to it. Changing alias affects original because they reference the same memory location. Both print statements show 100.

Aliasing a Function

The Name keyword can create alternative names for functions.

name_function.bas
Function CalculateSquare(n As Integer) As Integer
    Return n * n
End Function

Name Square As CalculateSquare

Print "Square of 5: "; Square(5)
Print "Square of 7: "; CalculateSquare(7)

We define a function CalculateSquare and then create a shorter alias Square for it. Both names can be used interchangeably to call the same function. This is useful for creating more concise names.

Aliasing a Type

Types can also be aliased using the Name keyword.

name_type.bas
Type Point
    x As Integer
    y As Integer
End Type

Name Coordinate As Point

Dim p As Point
Dim c As Coordinate

p.x = 10: p.y = 20
c = p
Print "c.x: "; c.x, "c.y: "; c.y

This example creates a Point type and then makes Coordinate an alias for it. Variables of both types are compatible because they refer to the same underlying type. The assignment between them works seamlessly.

Multiple Aliases

You can create multiple aliases for the same entity.

name_multiple.bas
Dim counter As Integer = 0
Name cnt As counter
Name total As counter

cnt = 5
total += 3
Print "counter: "; counter
Print "cnt: "; cnt
Print "total: "; total

Here we create two aliases (cnt and total) for the same variable. Changes through any name affect the same underlying variable. All three print statements show 8 as the final value.

Aliasing Array Variables

The Name keyword works with array variables as well.

name_array.bas
Dim numbers(1 To 5) As Integer
Name nums As numbers

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

Print "numbers(3): "; numbers(3)
Print "nums(3): "; nums(3)

We create an array numbers and alias it as nums. The alias can be used exactly like the original array name. Both print statements display 30, showing they access the same array elements.

Aliasing with Different Scopes

Aliases can be created in different scopes than their originals.

name_scope.bas
Dim Shared globalVar As Integer = 100

Sub TestSub()
    Name localAlias As globalVar
    localAlias = 200
End Sub

TestSub()
Print "globalVar: "; globalVar

This demonstrates aliasing a global variable with a local name in a subroutine. The alias localAlias modifies the original globalVar. The print statement shows the changed value of 200.

Aliasing Built-in Functions

You can create aliases for FreeBasic's built-in functions.

name_builtin.bas
Name Write As Print
Name StrLen As Len

Write "Hello, there!"
Write "Length: "; StrLen("FreeBasic")

Here we create shorter names for Print and Len functions. Write becomes an alias for Print, and StrLen for Len. This can make code more concise but should be used judiciously.

Best Practices

This tutorial covered the FreeBasic Name keyword with practical examples showing its usage for creating aliases in different contexts.

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.