text
stringlengths
0
275
LET v$ = FN Clamp$(15, 0, 10) ' ✓ OK
FN Clamp$(5, 1, 10) ' ✗ ERROR — return value unused
```
- Isolated scope: no access to global variables, only global constants (`#`)
- Parameters passed **by value**
- Labels inside functions are local — GOTO/GOSUB cannot jump outside
- Supports recursion
- Use `INCLUDE` to load functions from separate files if many
---
## String Functions
| Function | Description |
|----------|-------------|
| `ASC(s$)` | ASCII code of first char |
| `CHR(n)` | Character from ASCII code |
| `INSTR(s$, search$)` | Position (1-based), 0=not found |
| `INSTR(start, s$, search$)` | Search from position |
| `INVERT(s$)` | Reverse string |
| `LCASE(s$)` / `UCASE(s$)` | Lower / upper case |
| `LEFT(s$, n)` / `RIGHT(s$, n)` | First/last n chars |
| `LEN(s$)` | String length |
| `LTRIM(s$)` / `RTRIM(s$)` / `TRIM(s$)` | Strip whitespace |
| `MID(s$, start)` | Substring from start (1-based) |
| `MID(s$, start, len)` | Substring with length |
| `REPEAT(s$, n)` | Repeat string n times |
| `REPLACE(s$, a$, b$)` | Replace a$ with b$ in s$ |
| `SPLIT(arr$, s$, sep$)` | Split into array, returns count |
| `SRAND(n)` | Random alphanumeric string of length n |
| `STR(n)` | Number to string |
| `VAL(s$)` | String to number |
| `SHA256(s$)` | SHA256 hash (64-char hex) |
| `BASE64ENCODE(s$)` / `BASE64DECODE(s$)` | Base64 encode/decode |
---
## Math Functions
| Function | Description |
|----------|-------------|
| `ABS(n)` | Absolute value |
| `ATAN(n)` | Arc tangent |
| `BETWEEN(n, min, max)` | TRUE if min ≤ n ≤ max |
| `CEIL(n)` / `FLOOR(n)` | Round up / down |
| `CINT(n)` | Round to nearest integer |
| `CLAMP(n, min, max)` | Constrain n to [min, max] |
| `COS(n)` / `SIN(n)` / `TAN(n)` | Trig (radians) |
| `DEG(rad)` / `RAD(deg)` | Radians ↔ degrees |
| `DISTANCE(x1,y1, x2,y2)` | 2D Euclidean distance |
| `DISTANCE(x1,y1,z1, x2,y2,z2)` | 3D Euclidean distance |
| `EXP(n)` | e^n |
| `INT(n)` | Truncate toward zero |
| `LERP(start, end, t)` | Linear interpolation (t: 0.0–1.0) |
| `LOG(n)` | Natural logarithm |
| `MAX(a, b)` / `MIN(a, b)` | Larger / smaller of two |
| `MOD(a, b)` | Remainder |
| `POW(base, exp)` | Power |
| `RND(n)` | Random integer 0 to n-1 |
| `ROUND(n)` | Standard rounding |
| `SGN(n)` | Sign: -1, 0, or 1 |
| `SQR(n)` | Square root |
**Math constants:** `PI`, `HPI` (PI/2), `QPI` (PI/4), `TAU` (PI*2), `EULER`
---
## Graphics
```basic
SCREEN 12 ' 640×480 VGA (recommended)
SCREEN 0, 800, 600 ' Custom size
SCREEN 0, 1024, 768, "My Game" ' Custom size + title
FULLSCREEN TRUE ' Borderless fullscreen (graphics only)
FULLSCREEN FALSE ' Windowed
```
| Mode | Resolution |
|------|-----------|
| 1 | 320×200 |
| 2 | 640×350 |
| 7 | 320×200 |
| 9 | 640×350 |
| 12 | 640×480 ← recommended |
| 13 | 320×200 |
### Drawing Primitives
```basic
PSET (x, y), color ' Pixel
LINE (x1,y1)-(x2,y2), color ' Line
LINE (x1,y1)-(x2,y2), color, B ' Box outline
LINE (x1,y1)-(x2,y2), color, BF ' Box filled (FAST — use instead of CLS)
CIRCLE (cx,cy), radius, color ' Circle outline
CIRCLE (cx,cy), radius, color, 1 ' Circle filled
PAINT (x, y), fillColor, borderColor ' Flood fill
LET c$ = POINT(x, y) ' Read pixel color
LET col$ = RGB(r, g, b) ' Create color (0–255 each)
```