2016-06-28 19:35:31 +02:00
|
|
|
# Call a function
|
|
|
|
|
|
|
|
The simplest way to get started in JavaScript with your micro:bit is to
|
|
|
|
call one of the micro:bit's built-in JavaScript functions. Just like Blocks
|
|
|
|
are organized into categories/drawers, the micro:bit functions are organized by
|
2016-07-06 02:35:30 +02:00
|
|
|
namespaces, with names corresponding to the drawer names.
|
|
|
|
The `basic` namespace contains a number of very helpful functions:
|
2016-06-28 19:35:31 +02:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
basic.showString("Hello!")
|
|
|
|
```
|
|
|
|
|
2016-07-06 02:35:30 +02:00
|
|
|
If you want to see all functions available in the `basic` namespace, simply type `basic`
|
|
|
|
followed by `.` and a list of all the functions will appear.
|
2016-06-28 19:35:31 +02:00
|
|
|
|
2016-06-30 19:10:15 +02:00
|
|
|
![](/static/mb/js/basicFuns.png)
|
2016-06-28 19:35:31 +02:00
|
|
|
|
2016-07-06 02:35:30 +02:00
|
|
|
This feature is known as "Intellisense". Continue typing to select one of the functions,
|
2016-07-03 00:55:00 +02:00
|
|
|
or click on one of the functions to select. You also narrow down the set of functions by typing, as below:
|
2016-06-28 19:35:31 +02:00
|
|
|
|
|
|
|
![](/static/mb/js/basicIntell.png)
|
|
|
|
|
2016-07-06 02:35:30 +02:00
|
|
|
You can type anything to see what Intellisense will find for you. Here's an example
|
|
|
|
of what happens when you type the word `for`:
|
|
|
|
![](/static/mb/js/forIntell.png)
|
|
|
|
|
2016-07-03 00:55:00 +02:00
|
|
|
## Function parameter values
|
2016-06-28 19:35:31 +02:00
|
|
|
|
2016-07-03 00:55:00 +02:00
|
|
|
You might have noticed that the call `showString` above takes one parameter value,
|
2016-06-28 19:35:31 +02:00
|
|
|
the string to be scrolled on the LED screen. There is a second (optional)
|
2016-07-03 00:55:00 +02:00
|
|
|
parameter that controls the speed of the scroll. Try this:
|
2016-06-28 19:35:31 +02:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
basic.showString("Hello!",50)
|
|
|
|
```
|
|
|
|
|
2016-07-03 00:55:00 +02:00
|
|
|
Intellisense shows all the available parameters for a function.
|
2016-07-02 16:18:39 +02:00
|
|
|
|
2016-07-03 00:27:58 +02:00
|
|
|
## Left and right parentheses, please!
|
|
|
|
|
|
|
|
Whenever you want to call a function, you give the name of the function
|
|
|
|
followed by `(` and ending with `)`. Inbetween the left and right
|
|
|
|
parentheses go the function arguments. If a function has zero arguments, you still
|
2016-07-03 00:55:00 +02:00
|
|
|
need the parentheses in order to call the function. For example
|
2016-07-03 00:27:58 +02:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
basic.clearScreen()
|
|
|
|
```
|
2016-06-28 19:35:31 +02:00
|
|
|
|
2016-07-03 00:55:00 +02:00
|
|
|
It's a syntax error to have a left parenthesis without the "closing" right parenthesis:
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
basic.clearScreen(
|
|
|
|
```
|
|
|
|
|
2016-07-02 16:18:39 +02:00
|
|
|
### ~button /js/sequence
|
|
|
|
NEXT: Sequencing Commands
|
|
|
|
### ~
|