JS Built-in Functions

Built-in Functions

We are ready to cover some basic built-in functions to get us started. These functions are built inside both the BrainPad itself and in the programming language we’re using.

Prerequisite

This lesson assumes you’re already completed the Coding Intro lesson.

Intro

Built-in functions, sometimes referred to as API (Application Programming Interface), are functions that are pre-made and found internally in the system. Just like how we make a function for later use, someone created functions that are already found in the device that we can just use.

Device Specific

Some of these functions are provided by the specific devices’ libraries. Print() is one of them. The Print() function outputs whatever argument it is given to the screen.

await BrainPad.System.Println("Hello");
await BrainPad.System.Println(5);
This image has an empty alt attribute; its file name is BrainPadPulse_API_1.gif

Another function is Wait(). Just as you may be thinking, Wait() forces the BrainPad to pause for some time. The following code will output 1 and then pause for one second, then it outputs 2 and pauses for another second, and finally ends with output of 3.

await BrainPad.System.Println(1);
await BrainPad.System.Wait(1000);
await BrainPad.System.Println(2);
await BrainPad.System.Wait(1000);
await BrainPad.System.Println(3);

The complete list of device-specific functions are found on the Due Link website, for JavaScript.


Language Built-ins

There are some functions that are built-in as part of the programming languages we’re using. While languages may have different definitions of those functions, they provide a similar outcome.

For example, one of these functions is a random number generator. We will show how it is used in C# and in Python.

let myRandomNumber = Math.floor(Math.random()*100);
await BrainPad.System.Println(myRandomNumber);
myRandomNumber = Math.floor(Math.random()*100);
await BrainPad.System.Println(myRandomNumber);

In C#, Random object comes from the System namespace, needing System.Random(). Python needs an import of the random library. From there, the use is almost identical. C# Next() and Python randint() return the next random number.

The argument in the above example (the 100) tells the function to give us a random number that is no more than 100.

Can you use those random numbers to plot points or shapes on the screen at random locations? Just keep the shapes within the screen by limiting the random number to a max of the screen size. Don’t know how to draw yet? Well, that is next!


BrainPad.Display.Clear(0);
while(true){
    var x = Math.random()*127;
    var y = Math.random()*64;
    await BrainPad.Display.SetPixel(1,x, y);
    await BrainPad.Display.Show();
}

What’s Next?

You now know enough coding and you deserve to start with some fun topics. The next step is to learn about the drawing function.

BrainStorm

Do animals and humans have built in functions at birth? Why do some newborn animals get up as soon as they are born and start following their mom? Why does a parent is willing to risk their life to protect their children? Is this something they learned or it is built in our brains?

Content Licensing
Newsletter

Twitter Feed
Hot off the press!