Variables are – as their name suggests – variable, meaning changeable. More specifically, it’s their content that is changeable.
For example, you can store the number 10 in your sketch to output it in the Serial Monitor – just as you did with the text hello, world.
Serial.print(10);
But you can also use a variable and store the number there:
int zahl = 10;
If you then want to output the content of this variable in the Serial Monitor, you replace this “fixed” number with the variable:
Serial.print(zahl);
You won’t see any difference in the Serial Monitor – the result is the same. However, when programming, this makes a big difference! Imagine you want to use the number in multiple places in your sketch. One day, you want to replace it with an 11. That would mean you’d have to change every single place in the sketch where it’s entered.
However, if you use a variable, you only need to do this once.
int zahl = 11;
Everywhere you’ve used the variable, it will now use your new number.
In C++ there are a variety of variable types, for example for texts (strings), integers, and decimal numbers. In the next lesson, we’ll look at these types in more detail.