Category:Variables

Assigns the input value to a variable.

Variables are used as placeholders for values such as numbers or strings of text. Variables allow for a lot of freedom in programming. Instead of having to type out a phrase many times or remember a number, computer scientists can use variables to reference them. Variable values can change throughout a program.

If no variable with the specified name has been created, this block can create a new named variable with the given value. If the variable already exists, its new value will replace its old value.

Examples

Example 1

Change the color input given to the variable “spriteColor.” See how changing the variable value changes what the variable then passes to the change color block.

Example 2

A variable can be used several times in a program. Here, the variable “spriteSize” sets all the food sprites to the same size.

Example 3

If the variable value changes, it only affects the code after it. In this case, "spriteSize" passes one value to the apple and mushroom. It is then set to a new value that it is used in the code for the cherry and beet, but not in the code before it.

Parameters

NameDescription
value input The value the variable is being set to
variable name The name of the variable. The name can be chosen from a drop-down list or customized using the rename option.

Tips

  • Using a descriptive variable name helps best identify a specific variable and makes it easier for anyone else reading the code to understand what is happening.
  • If you decide to change a variable name while working on your project, you don’t have to change all the other matching blocks individually. Choosing to “rename all” variables with that name will automatically rename all the matching blocks used in your project.

Found a bug in the documentation? Let us know at documentation@code.org