---
title: Variables
description: 
published: 1
date: 2021-12-14T12:32:26.605Z
tags: 
editor: markdown
dateCreated: 2020-12-17T18:54:38.681Z
---

# Variables

| ![][menu]   | <br> [![][b1]][d1]&nbsp;&nbsp;&nbsp; [![][b2]][d2]&nbsp;&nbsp;&nbsp; <br> [![][b3]][d3]&nbsp;&nbsp;&nbsp; [![][b4]][d4]&nbsp;&nbsp;&nbsp; <br> [![][b5]][d5]|
|---|:---|

<br><br>

<!--- (COMMENT - INPUT Category Images (Used above)
-->
[spacer]: /reference_manual/variables/spacer.png
[menu]: /reference_manual/menus/menu_variables_70p.png
[b1]: /reference_manual/variables/addvar.png
[b2]: /reference_manual/variables/delvar.png
[b3]: /reference_manual/variables/set_50p.png
[b4]: /reference_manual/variables/change_50p.png
[b5]: /reference_manual/variables/initlocal_50p.png

<!--- (COMMENT - INPUT Category Detail Bookmarks (Used above)
-->
[d1]: /reference_manual/variables#add-a-variable
[d2]: /reference_manual/variables#delete-a-variable
[d3]: /reference_manual/variables#set-variable
[d4]: /reference_manual/variables#change-variable
[d5]: /reference_manual/variables#initialize-local


## add a variable
## Tab {.tabset}
### Description
![](/reference_manual/variables/addvar.png)
This block is actually a button. It creates a new global variable. 

If a variable with the same name exists, the new one will be created with the same name with the number 2 appended.
e.g.: variable with name **test** exists. Another one is created with the same name. The new one's name becomes **test2**.

### Example
The two pictures below depict the creation of **test** variable, followed by its appearance in the variables list.

![](/reference_manual/variables/code_createvar.png)
![](/reference_manual/variables/code_createvar2.png)


## <br><br>delete a variable
## Tab {.tabset}
### Description
![](/reference_manual/variables/delvar.png)
This block is actually a button. It is normally not visible, unless at least one variable has been created. Once a variable is created, this button is used to delete it. 

### Example
The two pictures below depict the variable deletion process. 

When DELETE A VARIABLE button is clicked, a list of all global variables is presented. Here variable **total** is to be deleted. It is selected and clicked. 

![](/reference_manual/variables/code_deletevar.png)

The second picture is the status of the variables display with the remaining variables.

![](/reference_manual/variables/code_createvar2.png)


## <br><br>set variable
## Tab {.tabset}
### Description
![](/reference_manual/variables/set.png)
This block is used to assign values to variables. Its selection menu will display a list of all global and local variables. 

![](/reference_manual/variables/set2.png)

To display the names of the local variables in the selection menu, this block has to be physically attached to the sequence of blocks where the INITALIZE LOCAL block is used.

![](/reference_manual/variables/set3.png)

In addition to displaying the names of the variables, the selection menu also displays an option for adding a variable. This can be used to add variables while editing the program code, without switching to the variables category options.

### Example
The example code shows the creation of a local variable **var** and assignment of value 123 to the global variable  **test**.

![](/reference_manual/variables/code_setvar.png)

[Download Link](/reference_manual/examples/code_setvar.ubp)


## <br><br>change variable
## Tab {.tabset}
### Description
![](/reference_manual/variables/change.png)
This block changes value of any variable, global or local, by the amount specified in the input area. The change amount can be a positive or a negative number. 

### Example
The program changes values of the two variables **test** and **var**.

![](/reference_manual/variables/code_changevar.png)

[Download Link](/reference_manual/examples/code_changevar.ubp)


## <br> Advanced
## <br>initalize local
## Tab {.tabset}
### Description
![](/reference_manual/variables/initlocal.png)
This block is used to create and initialize local variables. 

The default variable name **var** can be changed to anything else by clicking on the name and typing in a new name in the dialog box that opens up.

![](/reference_manual/variables/initlocal2.png)

Later on, if the local var's value needs to be changed, the SET VARIABLE block from the variables category can be used.


### Global vs Local Variables
A **global** variable has:

  1. **Global scope**: A global variable can be used in any script that doesn't have local variable of the same name that overrides it.

  2. **Long lifetim**e: A global variable is created explicitly and lives until it is explicitly deleted. It retains it's value as scripts start and stop and even when no scripts are running. However, clicking the "stop" button clears all global variables to the value zero. Global variables are also initialized to zero when they are first created and when a project is loaded.

In contrast, a **local** variable has:

  1. **Local Scope**: A local variable can be used only in the script where it appears. If several scripts use local variables with the same name, those variables are independent of each other.

  2. **Limited lifetime**: A local variable for a script is created when the script is started and deleted when the script terminates. A new local variable is created each time a script (including a function script) is started, and the local variables of every script invocation are independent of each other. (This is what makes recursion possible.)

  3. **Precedence over globals**: If a local variable has the same name as a global variable, the local variable overrides the global in the script in which the local variable appears. A variable is local in the entire script no matter where in the script the "initialize local var to" appears (although it is good coding practice for the "initialize local var to" to precede any other references to that variable).

> Everything about local variables also applies to the parameter variables of a function. You can think of parameters as local variables that were pre-initialized by the calling script.
{.is-info}


### Example
See examples above.



