text
stringlengths
0
721
---
## Libraries & INCLUDE
```basic
INCLUDE "helpers.bas" ' Insert source at this point
INCLUDE "MathLib.bb" ' Load compiled library
' Compile library (functions only no loose code)
' bazzbasic.exe -lib MathLib.bas → MathLib.bb
' Function names auto-prefixed: MATHLIB_functionname$
PRINT FN MATHLIB_add$(5, 3)
```
Library functions can read main-program constants (`#`). `.bb` files are version-locked.
---
## Passing Arrays to Functions
Arrays cannot be passed directly to `DEF FN` functions, but a clean workaround exists using JSON serialization. Convert the array to a JSON string with `ASJSON`, pass the string as a parameter, then deserialize inside the function with `ASARRAY`. This is the accepted pattern in BazzBasic v1.2+.
```basic
DEF FN ProcessPlayer$(data$)
DIM arr$
LET count$ = ASARRAY(arr$, data$)
RETURN arr$("name") + " score:" + arr$("score")
END DEF
[inits]
DIM player$
player$("name") = "Alice"
player$("score") = 9999
player$("address,city") = "New York"
[main]
LET json$ = ASJSON(player$)
PRINT FN ProcessPlayer$(json$)
END
```
**Notes:**
- The function receives a full independent copy changes inside do not affect the original array
- Nested keys work normally: `arr$("address,city")` etc.
- Overhead is similar to copying an array manually; acceptable for most use cases
---
## Program Structure
```basic
' ---- 1. FUNCTIONS (or INCLUDE "functions.bas/bb") ----
DEF FN Clamp$(v$, lo$, hi$)
IF v$ < lo$ THEN RETURN lo$
IF v$ > hi$ THEN RETURN hi$
RETURN v$
END DEF
' ---- 2. INIT (declare ALL constants & variables here, not inside loops) ----
' Performance: variables declared outside loops avoid repeated existence checks.
[inits]
LET SCREEN_W# = 640
LET SCREEN_H# = 480
LET MAX_SPEED# = 5
SCREEN 0, SCREEN_W#, SCREEN_H#, "My Game"
LET x$ = 320
LET y$ = 240
LET running$ = TRUE
' ---- 3. MAIN LOOP ----
[main]
WHILE running$
IF INKEY = KEY_ESC# THEN running$ = FALSE
GOSUB [sub:update]
GOSUB [sub:draw]
SLEEP 16
WEND
SOUNDSTOPALL
END ' place at the end of main program so BazzBasic wont fall to subs in runtime
' ---- 4. SUBROUTINES (or INCLUDE "subs.bas") ----
[sub:update]
IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - MAX_SPEED#
IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + MAX_SPEED#
RETURN
[sub:draw]
SCREENLOCK ON
LINE (0,0)-(SCREEN_W#, SCREEN_H#), 0, BF
CIRCLE (x$, y$), 10, RGB(0, 255, 0), 1
SCREENLOCK OFF
RETURN
```
**Key conventions:**
- Variables: `camelCase$` | Constants: `UPPER_SNAKE_CASE#` | Functions: `PascalCase$`
- Labels: `[gameLoop]` for jump targets, `[sub:name]` for subroutines