pxt-calliope/docs/blocks/variables/var.md

88 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2016-03-26 00:47:20 +01:00
# Local Variables
2016-04-02 01:22:47 +02:00
How to define and use local variables.
2016-03-26 00:47:20 +01:00
### @parent language
2016-04-16 00:02:26 +02:00
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/reference/types), and value:
2016-03-26 00:47:20 +01:00
* *name* is how you'll refer to the variable
* *type* refers to the kind of data a variable can store
* *value* refers to what's stored in the variable
### Var statement
2016-04-16 00:02:26 +02:00
Use the Block Editor variable statement to create a variable
and the [assignment operator](/blocks/variables/assign)
2016-04-16 00:02:26 +02:00
to store something in the variable.
2016-03-26 00:47:20 +01:00
2016-04-16 00:02:26 +02:00
For example, this code stores the number `2` in the `x` variable:
2016-03-26 00:47:20 +01:00
2016-04-16 00:02:26 +02:00
```blocks
let x = 2;
```
2016-03-26 00:47:20 +01:00
Here's how to define a variable in the Block Editor:
1. Click `variables`.
2. Change the default variable name if you like.
3. Drag a block type on the right-side of the [assignment operator](/blocks/variables/assign) and click the down arrow to change the variable name.
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
A variable is created for the number returned by the [brightness](/reference/led/brightness) function.
2016-03-26 00:47:20 +01:00
2016-04-16 00:02:26 +02:00
```blocks
let b = led.brightness();
```
2016-03-26 00:47:20 +01:00
### Using variables
Once you've defined a variable, just use the variable's name whenever you need what's stored in the variable. For example, the following code shows the value stored in `counter` on the LED screen:
2016-04-16 00:02:26 +02:00
```blocks
let counter = 1;
basic.showNumber(counter);
```
2016-03-26 00:47:20 +01:00
To change the contents of a variable use the assignment operator. The following code sets `counter` to 1 and then increments `counter` by 10:
2016-08-09 17:39:50 +02:00
```blocks
2016-04-16 00:02:26 +02:00
let counter = 1;
counter = counter + 10;
basic.showNumber(counter);
```
2016-03-26 00:47:20 +01:00
### Why use variables?
2016-04-16 00:02:26 +02:00
If you want to remember and modify data, you'll need a variable.
A counter is a great example:
2016-03-26 00:47:20 +01:00
2016-04-16 00:02:26 +02:00
```blocks
let counter = 0;
input.onButtonPressed(Button.A, () => {
counter = counter + 1;
basic.showNumber(counter);
});
```
2016-03-26 00:47:20 +01:00
### Local variables
Local variables exist only within the function or block of code where they're defined. For example:
2016-04-16 00:02:26 +02:00
```blocks
// x does NOT exist here.
if (led.brightness() > 128) {
// x exists here
let x = 0;
}
```
2016-03-26 00:47:20 +01:00
#### Notes
* You can use the default variable names if you'd like, however, it's best to use descriptive variable names. To change a variable name in the editor, select the down arrow next to the variable and then click "new variable".
### See also
[types](/reference/types), [assignment operator](/blocks/variables/assign)
2016-03-26 00:47:20 +01:00