BSL Data & Functions

As your scripts get larger, writing everything line-by-line becomes difficult to manage. This section introduces how to group multiple pieces of data together and how to wrap your code into reusable blocks.

BSL Arrays

An array is a special variable that can hold more than one value at a time. Instead of creating separate variables for every single item, you can store them all in one array and access them using an index number.

Creating an Array
In BSL, you can create an array using literal syntax [] or by using the new Array(size) command. Arrays are zero-indexed, meaning the first item is at index 0, the second is at index 1, and so on.

The Auto-Resize Feature
One of the most powerful features of BSL arrays is that they auto-resize. If you assign a value to an index that is larger than the array's current size, BSL will automatically grow the array to fit it. Any empty slots created during this expansion are automatically initialized with a value of 0.

Getting Array Length using sizeof()
To find out how many items are currently in an array, use the built-in sizeof() function.

// Try it Yourself:

// Create an array with an initial size of 2 (Slots: [0, 0])
var arr = new Array(2); 

// Assign a value to the first slot (Index 0)
arr[0] = 42;

// Assigning to Index 5 forces the array to auto-resize!
// Slots 1, 2, 3, and 4 will automatically be filled with 0
arr[5] = 99; 

print("Array length is now: " + sizeof(arr));

// Loop through the array to see its contents
var j;
for (j = 0; j < sizeof(arr); j++) {
    print("arr[" + j + "] = " + arr[j]);
}

Output

Array length is now: 6
arr[0] = 42
arr[1] = 0
arr[2] = 0
arr[3] = 0
arr[4] = 0
arr[5] = 99

BSL Functions

A function is a block of code designed to perform a particular task. A function is executed when something "calls" (invokes) it.

Why use Functions?
You can reuse code! Define the code once, and use it many times with different arguments to produce different results.

Function Syntax
A BSL function is defined with the function keyword, followed by a name, and parentheses ().Any parameters (inputs) are placed inside the parentheses. The code to be executed is placed inside curly brackets {}.

Returning Values
When BSL reaches a return statement, the function will stop executing and return the specified value back to the place where it was called.

// Try it Yourself:

// Define a function that adds two numbers
function add(x, y) {
    return x + y;
}

// Call the function and store the result in a variable
var result = add(5, 10);
print("5 + 10 = " + result);

// Call it again with different numbers!
print("100 + 250 = " + add(100, 250));

Functions Without a Return Value
Not all functions need to return data back to the caller. Sometimes, you just want a function to perform an action, like printing a message to the screen or modifying an existing variable.
If a function does not have a return statement, it simply executes its code block and finishes. (Behind the scenes, BSL will silently return a null value).

// Try it Yourself:

// Define a function that just performs an action (no return)
function greetUser(name) {
    print("Welcome to the system, " + name + "!");
    print("We hope you enjoy learning BSL.");
}

// Call the function
greetUser("Alice");
greetUser("Bob");

Output

Welcome to the system, Alice!
We hope you enjoy learning BSL.
Welcome to the system, Bob!
We hope you enjoy learning BSL.

First-Class Functions
In BSL, functions are treated as first-class values. This means you can actually pass a function as an argument to another function!

// Try it Yourself:

// A function that takes another function (fn) and runs it twice
function callTwice(fn, value) {
    fn(value);
    fn(value);
}

// A simple function that prints a message
function printer(msg) { 
    print("System says: " + msg); 
}

// Pass the printer function into callTwice
callTwice(printer, "Hello!");

Output

System says: Hello!
System says: Hello!