← All journal articles

What is a variable in programming

Variable in programming

A variable is a named or otherwise addressed area of memory that can be used to access data. It sounds complicated and scary, doesn't it? However, in practice, your child will not need such a definition. At least at the initial stages of learning programming.

In simple words, a variable is a data store. You can put a value here (for example, a number, string, or other data type). An even easier way to imagine a variable is to think about what's around us. For example, a variable can be a small bag where you can put, for example, an apple. It will stay there until we decide to do something about the apple.

A variable must be declared in any program. That is, get this bag before putting the apple in it. In modern applications, there can be as many variables as you like. For example, an apple is stored in one bag, and candy is stored in the other.

Why do we need variables and how do they work

Variables store certain data that can be later used in the program. In order for a variable to appear, it is necessary to declare it (reserve a memory location for certain data). Different languages declare variables differently depending on the syntax. Somewhere it could be the word “var”, somewhere it could be “let”. There are also immutable variables that are set only once and are declared as “const”.

Variables in the program are needed not only for storing data, but also for various operations with it. For example, you could create a simple calculator using just three variables — a, b, and c. How would this work? Without delving into the subtleties of the syntax of each individual programming language, we will show a simple example:

a+b =c

So we have three variables. The first two are used for calculation, and the third is used to record the value of the sum. Moreover, the first two variables may have specified initial values, or they may be empty (that is, the variable is declared, but no value is assigned to it). In the second case, you can substitute the values yourself and calculations will be made based on them.

By the way, data can be recorded directly in the program. If we take our example above, we can immediately use numbers instead of Latin letters. For example, 1+2 = 3. However, in this case, the program you will write will not be flexible enough. That is, we can only calculate the specified values in it, and in order to change something, we will have to write new numbers in the code each time, and only then we will get a different result.

Variables allow you to add flexibility to the application. That is, we can change their values ourselves and get the required results each time without interfering with the program code.

Variables in Scratch

When working with the Scratch development environment, variables are also used. There is a separate section where you can create them. When you click on a special button, we will see a window where you can set the name and value of the variable. Scratch also has the ability to determine the scope, that is, whether the variable will be available only to one sprite or to all.

It is important to remember that if a variable is available to all sprites, it must have a unique name. Two variables with the same name cannot exist in the same scope.

And if you plan to use variables in different areas of view (sprites), you can use the same names here.

How do you name variables? You can just use Latin letters. However, the most popular and effective option is descriptive naming so that when we work with code in the future, we can know exactly what value is in the variable and what to “put” there if such actions are planned. When learning through scratch, it is better to use English variable names right away. The fact is that in adult programming, all code is written exclusively in English, and in the future, if a child works with foreign companies in the future, he will have to learn this language.

For example, we have a variable in which some animals are entered. You can call it “Zhivotnye”, but it's better to write “Animals” right away.

What can variables be used for in Scratch? In general, for any program we write here. For example, we have two characters and they have to move across the screen. We can set their speed manually, or we can enter this data into a variable and then change the speed for both characters at the same time. This will be more effective.

In addition to speed, other dynamic parameters, such as the number of steps, can also act as variables. And these are not all possibilities.

findings

A variable is the basis for any programming language. It is necessary for quick access to and manipulation of data. In classes at ProgKids, your child will learn more about variables and learn how they are applied in practice.

You can also read

Courses for kids

Progkids обратная связь

It's easy to sign up for a free class

Already in the first lesson, we'll dive into the basics of development and create a small project that your child will want to brag about.

Submit a request

ok image
Ваша заявка отправлена. Скоро мы свяжемся с Вами
Ошибка при отправке формы