# Variables & Constants
- Variables store values that can change during program execution.
- Constants store value that can not be changed during execution

## Declaration
- Variables and constants must be initialized with `LET` before they can be used
- BazzBasic variables are not typed, but work the same way as in JavaScript, for example.
- A variable needs the suffix **$**
- A constant needs the suffix **#**

## Variables
```vb
' Basic init for variables
LET a$ = "Foo"
PRINT a$ 		' Output: "Foo"

LET b$ = 1
PRINT b$ 		' Output: 1

LET c$ = 1 + 2
PRINT c$ 		' Output: 3
```

## Constants
```vb
' Basic init for constants

LET GRAVITY# 		= 9.81
LET MAX_PLAYERS# 	= 4
LET GAME_TITLE# 		= "Space Invaders"
```

Built-in constants such as `PI#` already exist (see [Built-in Constants](#built-in-constants)) and cannot be redefined: `LET PI# = 3.14` stops the program with `Cannot redefine constant 'PI#'`. Give your own constants other names.

## LET only when init
```vb
' Once variable is initialized, you do not need LET anymore

LET a$ = 1

a$ = 3

PRINT a$ ' Output: 3
```

## Untyped data type
```vb
' Variable stores either a number or a text.
' It is ok to change the type of data.

LET a$ = 1
PRINT a$ ' Output: 1

a$ = "Foo"
PRINT a$ ' Output: Foo
```

## Versatile usage
```vb
' Math with variables and multiple inits in single line

LET a$ = 1, b$ = 2
LET c$ = a$ + b$

LET MY_CONST# = c$ * b$
```

## Error situation examples
```vb
' Some errors

a$ = 1 ' error: a$ not initialized

LET B# = 1
B# = B# + 3 ' error: Constant B# had value 1 when initialized, not allowed to change anymore
```

## Init with out a value
```vb
' If you want just to init var and not give value yet

LET a$ ' declares variable

a$ = 1 ' inits value 1 to variable

LET B# ' works, but is a bit stupid since now B# is constant with value of nothing
```

## Concatenated values
```vb
LET x$ = 10
x$ += 5     ' 15
x$ -= 3     ' 12
x$ *= 2     ' 24
x$ /= 4     ' 6
PRINT x$    ' 6

LET s$ = "Hello"
s$ += " World"
PRINT s$    ' Hello World
```

**Important:** Not allowed with constants "#"

## Exceptions

### FOR and INPUT
When a variable is introduced with a `FOR...NEXT` or `INPUT` command, it does not need to be initialized with `LET`:
```vb
REM name$ is initialized by INPUT
INPUT "What is your name? ", name$

REM i$ is initialized by FOR
FOR i$ = 1 TO 10
    PRINT i$
NEXT
```

## Comparing variables
A comparison is true if:
- two string variables are equal
- two number variables are equal
- the value of the number in the string variable is the same as the number variable

```vb
LET a$, b$, c$

a$ = "12"               ' a$ holds the text "12"
b$ = 12                 ' b$ holds the number 12
c$ = b$ + a$            ' c$ is now "1212" - + joins when one side is text

' Output of this IF...THEN: Same
IF a$ = b$ THEN
	PRINT "Same"
ELSE
	PRINT "Different"
ENDIF

' Output of this IF...THEN: Different
IF a$ = c$ THEN
	PRINT "Same"
ELSE
	PRINT "Different"
ENDIF

c$ = b$ + VAL(a$)       ' c$ is now 24 - VAL() turns the text into a number
' Output of this IF...THEN: Same
IF c$ = 24 THEN
	PRINT "Same"
ELSE
	PRINT "Different"
ENDIF
```

**`+` with text:** if either side of `+` is a string, `+` joins the values as text instead of adding them. It works from left to right, and `*` and `/` are calculated first.
```vb
PRINT "12" + 3                ' Output: 123
PRINT 1 + "23"                ' Output: 123
PRINT 1 + 2                   ' Output: 3
PRINT 1 + 2 + "Foo" + 4 * 5   ' Output: 3Foo20
```

**Note:** Although 123 and "123" produce TRUE in an IF...THEN comparison, it is best to keep numeric variables as numeric and string variables as string.

A comparison between a numeric and string variable is only made if the data type of the variable contents is not the same. This slows down the comparison process if it is done significantly.

## Built-in Constants
BazzBasic provides a full range of automatically initialized constants

**Math:**
| Constant | Description |
|----------|-------------|
| `PI#`, `HPI#`, `QPI#`, `TAU#` | π, π/2, π/4 and 2π — see [Math Functions](math_functions.md) |
| `EULER#` | e ≈ 2.718281828459045 |
| `AU#` | 149597870700 — astronomical unit in metres (exact, IAU 2012) |

> **Note:** if older code declares its own `AU#` with `LET`, remove that line — built-in constants cannot be redefined, so it is now an error.

**Physics:**
| Constant | Description |
|----------|-------------|
| `PHI#` | 1.618033988749895 — golden ratio φ (since 1.8) |
| `GRAV#` | 6.6743E-11 — gravitational constant, m^3 kg^-1 s^-2 (since 1.8) |
| `GRAV_E#` | 9.80665 — standard gravity on Earth, m/s^2 (since 1.8) |

```vb
PRINT PHI#      ' Output: 1.618033988749895
PRINT GRAV_E#   ' Output: 9.80665
```

**Colors:**
| Constant | Value (ARGB) | RGB |
|----------|--------------|-----|
| `BLACK#` | 0xFF000000 | (0, 0, 0) |
| `WHITE#` | 0xFFFFFFFF | (255, 255, 255) |
| `RED#` | 0xFFFF0000 | (255, 0, 0) |
| `GREEN#` | 0xFF00FF00 | (0, 255, 0) |
| `BLUE#` | 0xFF0000FF | (0, 0, 255) |
| `YELLOW#` | 0xFFFFFF00 | (255, 255, 0) |
| `CYAN#` | 0xFF00FFFF | (0, 255, 255) |
| `MAGENTA#` | 0xFFFF00FF | (255, 0, 255) |
| `GRAY#` | 0xFF808080 | (128, 128, 128) |
| `ORANGE#` | 0xFFFFA500 | (255, 165, 0) |
| `TRANSPARENT#` | 0x00000000 | (0, 0, 0, alpha 0) |

Use them as the `color` parameter of drawing commands (`PSET`, `LINE`, `CIRCLE`, `PAINT`, `DRAWSTRING`, `LOADSHAPE`) — like `RGB(r, g, b)` values they are 32-bit ARGB. `COLOR` (console text colors) still takes a palette index 0–15 only. Since 1.8.

```vb
SCREEN 320, 200
LINE (10, 10)-(100, 100), RED#
CIRCLE (160, 100), 40, YELLOW#
```

**System:**
| Constant | Description |
|----------|-------------|
| `PRG_ROOT#` | Full path to the program's base directory (always ends with a directory separator, so you can concatenate a relative path directly) |
| `BBVER#` | BazzBasic version string (e.g. `"1.1d"`) |
| `BBBUILD#` | Build time of the interpreter, `"dd-mm-yyyy hh:mm:ss"` (e.g. `"06-11-2026 20:45:57"`). A program compiled with `-exe` reports the build time of the interpreter it was made with, not the time it was compiled |

```vb
PRINT "Version: "; BBVER#     ' Output: 1.1d
PRINT "Build:   "; BBBUILD#   ' Output: 06-11-2026 20:45:57
PRINT "Root:    "; PRG_ROOT#  ' Output: C:\path\to\program\
```

**Keyboard:**

### Arrow Keys
| | | |
|---|---|---|
| `KEY_UP#` | `KEY_DOWN#` | `KEY_LEFT#` |
| `KEY_RIGHT#` | | |

### Special Keys
| | | |
|---|---|---|
| `KEY_ESC#` | `KEY_TAB#` | `KEY_BACKSPACE#` |
| `KEY_ENTER#` | `KEY_SPACE#` | `KEY_INSERT#` |
| `KEY_DELETE#` | `KEY_HOME#` | `KEY_END#` |
| `KEY_PGUP#` | `KEY_PGDN#` | |

### Modifier Keys
| | | |
|---|---|---|
| `KEY_LSHIFT#` | `KEY_RSHIFT#` | `KEY_LCTRL#` |
| `KEY_RCTRL#` | `KEY_LALT#` | `KEY_RALT#` |
| `KEY_LWIN#` | `KEY_RWIN#` | |

### Function Keys
| | | |
|---|---|---|
| `KEY_F1#` | `KEY_F2#` | `KEY_F3#` |
| `KEY_F4#` | `KEY_F5#` | `KEY_F6#` |
| `KEY_F7#` | `KEY_F8#` | `KEY_F9#` |
| `KEY_F10#` | `KEY_F11#` | `KEY_F12#` |

### Numpad Keys
| | | |
|---|---|---|
| `KEY_NUMPAD0#` | `KEY_NUMPAD1#` | `KEY_NUMPAD2#` |
| `KEY_NUMPAD3#` | `KEY_NUMPAD4#` | `KEY_NUMPAD5#` |
| `KEY_NUMPAD6#` | `KEY_NUMPAD7#` | `KEY_NUMPAD8#` |
| `KEY_NUMPAD9#` | `KEY_NUMPAD_PLUS#` | `KEY_NUMPAD_MINUS#` |
| `KEY_NUMPAD_MUL#` | `KEY_NUMPAD_DIV#` | `KEY_NUMPAD_DOT#` |

Numpad keys give the same value on every keyboard layout. Numpad Enter returns the same value as `KEY_ENTER#`.

### Punctuation Keys
| | | |
|---|---|---|
| `KEY_COMMA#` | `KEY_DOT#` | `KEY_MINUS#` |
| `KEY_EQUALS#` | `KEY_SLASH#` | `KEY_BACKSLASH#` |
| `KEY_SEP#` | `KEY_GRAVE#` | `KEY_LBRACKET#` |
| `KEY_RBRACKET#` | | |

> Punctuation keys depend on the keyboard layout. A character that needs Shift or AltGr on the user's keyboard (for example `=` and `/` on a Finnish keyboard) cannot be detected with these constants. See [Keyboard layouts](input_functions.md?id=keyboard-layouts-and-portable-controls).

### Alphabet Keys
| | | |
|---|---|---|
| `KEY_A#` | `KEY_B#` | `KEY_C#` |
| `KEY_D#` | `KEY_E#` | `KEY_F#` |
| `KEY_G#` | `KEY_H#` | `KEY_I#` |
| `KEY_J#` | `KEY_K#` | `KEY_L#` |
| `KEY_M#` | `KEY_N#` | `KEY_O#` |
| `KEY_P#` | `KEY_Q#` | `KEY_R#` |
| `KEY_S#` | `KEY_T#` | `KEY_U#` |
| `KEY_V#` | `KEY_W#` | `KEY_X#` |
| `KEY_Y#` | `KEY_Z#` | |

### Number Keys
| | | |
|---|---|---|
| `KEY_0#` | `KEY_1#` | `KEY_2#` |
| `KEY_3#` | `KEY_4#` | `KEY_5#` |
| `KEY_6#` | `KEY_7#` | `KEY_8#` |
| `KEY_9#` | | |

**Gamepad:** (see [Gamepad](input_functions.md?id=gamepad))

### Gamepad Buttons
| | | |
|---|---|---|
| `PAD_A#` | `PAD_B#` | `PAD_X#` |
| `PAD_Y#` | `PAD_UP#` | `PAD_DOWN#` |
| `PAD_LEFT#` | `PAD_RIGHT#` | `PAD_LB#` |
| `PAD_RB#` | `PAD_LSTICK#` | `PAD_RSTICK#` |
| `PAD_BACK#` | `PAD_START#` | `PAD_GUIDE#` |
| `PAD_MISC#` | `PAD_TOUCHPAD#` | |

### Gamepad Axes
| | | |
|---|---|---|
| `PAD_LX#` | `PAD_LY#` | `PAD_RX#` |
| `PAD_RY#` | `PAD_LT#` | `PAD_RT#` |

## Naming Rules
1. Must end with `$` (variable) or `#` (constant)
2. Can contain letters, numbers, and underscores
3. Cannot start with a number
4. Case-insensitive

### Valid names:
```vb
LET score$
LET player1_name$
LET MAX_VALUE#
LET x$
```

### Invalid names:
```vb
LET score       ' Missing suffix
LET 1player$    ' Starts with number
```

## Errors
In case of error, BazzBasic stops with proper error message.
```vb
LET a$ = 100 ' ok
b$ = 200     ' Error at line 2: Undefined variable: B$ (use LET for first assignment)
```

## Scope
Variables initialized within the main code belong to the same scope.
```vb
IF 1 = 1 THEN
    LET x$ = 10
END IF
PRINT x$                 ' Output: 10 (x$ is still accessible)
```

- Variables initialized and modified in user-defined functions do not belong to the same scope.
- However, global constants are available.
- Arrays follow the same scoping as variables - they are not global. To pass an array into a DEF FN function, pass it as a JSON string with `ASJSON()` and rebuild it inside with `ASARRAY()` (see [User-defined Functions](user-defined-functions.md)).

```vb
LET a$ = 1					' Global variable
LET b$ = 100              	' Global variable
LET C# = "Foo"            	' Global constant

DEF FN test$(a$)           	' Local parameter a$ is initialized here
    LET b$ = 2            	' Local b$ (separate from global b$)
    PRINT b$               	' Output: 2
    PRINT C#              	' Output: Foo
    a$ = a$ * b$           	' Uses local a$ (100) and local b$ (2)
    RETURN a$              	' Returns 200
END DEF

PRINT FN test$(b$)         	' Output: 200
PRINT a$                   	' Output: 1
PRINT b$                   	' Output: 100
```