← Documentation Home

1) Language Foundations

Back to Tutorials

Goal: write a small program that stores typed values, performs calculations, builds text, and calls a function.

Time: about 20 minutes. No assets or Pro license are required.


Step 1: Print a Diagnostic Message

Create a new LunarBasic file and enter:

DebugPrint("Hello from LunarBasic!")

Run the program. DebugPrint writes a line to the runtime diagnostics output, so it is ideal for checking values while learning.

Step 2: Declare and Assign Typed Variables

Dim introduces a variable. The As clause documents and enforces the kind of value it stores.

Dim playerName As String
Dim score As Integer
Dim speed As Double
Dim hasKey As Boolean

playerName = "Luna"
score = 125
speed = 3.5
hasKey = False

Use Integer for whole-number coordinates, counters, and handles; Double or Float for fractional calculations; String for text; and Boolean for True/False state.

Step 3: Build Expressions

Expressions combine values with operators. Parentheses make the intended order clear.

Dim coins As Integer
Dim bonus As Integer
Dim total As Integer

coins = 7
bonus = 25
total = (coins * 10) + bonus
DebugPrint("Total: " + Str(total))

Str converts a number to text before concatenation with +.

Step 4: Continue a Long Statement

Put a whitespace-separated underscore at the end of a physical line to continue the same statement on the next line.

Dim summary As String
summary = "Player: " + playerName + _
    ", score: " + Str(score) + _
    ", speed: " + Str(speed)

DebugPrint(summary)

Only whitespace may follow the underscore. Continuation also works in function declarations, argument lists, expressions, and parenthesized code.

Step 5: Work with String Positions and Characters

Dim title As String
Dim letterPosition As Integer
Dim ordinal As Integer

title = "LunarBasic"
letterPosition = Instr(title, "Basic", 1)
ordinal = Asc(title, 6)

DebugPrint("Length: " + Str(StringLen(title)))
DebugPrint("Basic starts at: " + Str(letterPosition))
DebugPrint("Character 6 ordinal: " + Str(ordinal))
DebugPrint("First five letters: " + Left$(title, 5))

Instr and Asc use 1-based offsets. ChrPos is different: it returns a zero-based index for a character search. Check the signature when mixing string functions.

Step 6: Add a Reusable Function

Function declarations use parentheses even when there are no parameters. A function returns a value with Return.

Function CalculateBonus(level As Integer, coins As Integer) As Integer
    Return (level * 100) + (coins * 10)
End Function

Dim result As Integer
result = CalculateBonus(3, 7)
DebugPrint("Bonus: " + Str(result))

Checkpoint: Complete Program

Replace your file with this complete checkpoint and run it:

Function CalculateBonus( _
    level As Integer, _
    coins As Integer _
) As Integer
    Return (level * 100) + (coins * 10)
End Function

Dim playerName As String
Dim message As String
Dim score As Integer

playerName = "Luna"
score = CalculateBonus(3, 7)
message = "Player " + playerName + _
    " earned " + Str(score) + " points."

DebugPrint(message)
DebugPrint("The ordinal value of L is " + Str(Asc(playerName, 1)))

You should see a score of 370 and an ordinal value of 76.

Common Mistakes

  • Text must use double quotes. An apostrophe begins a comment.
  • Convert numbers with Str before joining them to strings.
  • Place whitespace before a continuation underscore and nothing except whitespace after it.
  • Do not use a variable before assigning a meaningful value.

Try It Yourself

Add a second function named RankName that accepts a score and returns "Bronze", "Silver", or "Gold". You will learn the required If syntax in the next lesson.

Functions Introduced

DebugPrint, Str, StringLen, Instr, Asc, and Left$.


Navigation: Course outline | Next: Decisions, Data, and Routines