Functions

Functions contain code that can be reused throughout your program, which helps organize complex logic. Think of a function as a program within a program. Parameters specify different input values for your function; they are like a variable but only apply to the function, and they are not global to the rest of the program.

Start Program

You have probably noticed that every text program has a function called startProgram. This is a special function: Sphero expects the code inside startProgram to be the program's "main activity." For this reason, some programmers refer to startProgram as the 'main loop' in their code.

If your startProgram contains a loop, it must also contain a small delay, otherwise you will see a runtime error about spending too much time inside the function:

async function startProgram() {
    while (true) {
        // Your code here...
        
        await delay(0.033); // <-- without this, you will get a runtime error.
    }
}


If the loop inside startProgram calls functions that have built-in delays, you don't need the extra delay at the end.

Defining Functions

5 define functions

Functions can be defined anywhere in a program, but in the examples below we define them above the main loop for simplicity. Usually, the order in which functions appear in your program doesn't matter. Even if other functions are defined before the startProgram function, startProgram will be run first (there are exceptions to this, but they are outside the scope of this wiki).


See the below blink function example that changes the LED's to red for 2s, then green for 2s. If you call this function from startProgram, it does exactly what you expect, executing the logic in the function:

async function blink() {
    playSound(Sound.Effects.Click);
    setMainLed({ r: 255, g: 0, b: 0 });
    await delay(0.5);
    setMainLed({ r: 0, g: 255, b: 0 });
    await delay(0.5);
}

async function startProgram() {
    await blink();
}

Parameters

Parameters are like variables, but they are local to a function in that they only serve the function; they are not global to the rest of the program. Like variables, their four types are number, string, boolean, and color. Parameters are very helpful in quickly changing inputs without modifying the core function, or if you want to call a function many times and make changes to the input each time.

The below blinkParameters example extends blink function by enabling you to control the blink time and number via the parameters time and loop. You can call the function with different values each time. If you write this program without parameters it would require a lot more code:

async function blinkParams(time, loops) {
    for (var _i1 = 0; _i1 < loops; _i1++) {
        playSound(Sound.Effects.Click);
        setMainLed({ r: 255, g: 0, b: 0 });
        await delay(time);
        setMainLed({ r: 0, g: 255, b: 0 });
        await delay(time);
        await delay(0.025);
    }
}

async function startProgram() {
    await blinkParams(0.5, 5);
    await blinkParams(0.15, 10);
}

Notice that after you define a parameter or function it is added to the auto-fill engine when you call it later:

paramPredict