A variable in code is exactly like a variable in math. We can create variables to store a value that might change and refer to it elsewhere in our code. This can be used to keep our code simple and clean, or it might be used for calculations and comparisons.

Transcript

Hi Steven, here, welcome back to the Circuit Playground Express online workshop. In this section, we're going to learn about variables.

Now a variable in the code is just like a variable in math. It's a placeholder that you can store data in and reference in other parts of the code. Let's look at how it looks and MakeCode.

So, if we open up MakeCode we'll go to the variables section. Now when you first open it up there are no variables there, so we'll need to make a variable and I'm going to call mine, "there", just for just to keep things clear. So now we have a few options here, we can set our variable to a certain number, we can change it, or we can just reference it as if it's a number in this bubble. So, we're going to start by setting our variable, will say setting our variable to 1 and then oh no, we'll set our variable to sound level.

So, whenever this loop is run through the sound level is stored, as the variable there and then we'll take a light, the light section, will grab a graph and then we'll graph the variable "there", up to let's say 75. So, this code here is what I have running on the board and it's a sound level sensor and this isn't really the way that you'd use a variable because it's kind of redundant in this case, but just to give you an idea, every time it runs through we're reading the sound level, we're storing it as variable and then here we're referencing the variable and graphing it on the lights on the board.

We could then use "variable" any number of times throughout the program and get that sound level that's taken, at the front of every loop and this is necessary for a lot of distinct functions within programming, you use them every for-loop, which we'll go over in the next section and you, of course, use it for a lot of different math functions within code.

If you're an educator and you're teaching beginner programmers, you won't get into variables too much, unless you use the infrared transmitter and receiver, but it's important to know in case you run into it. So that concludes this section on variables stick around in the next section, we're going to learn about programming logic.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.