Discussing the article: "From Basic to Intermediate: Variables (II)"

 

Check out the new article: From Basic to Intermediate: Variables (II).

Today we will look at how to work with static variables. This question often confuses many programmers, both beginners and those with some experience, because there are several recommendations that must be followed when using this mechanism. The materials presented here are intended for didactic purposes only. Under no circumstances should the application be viewed for any purpose other than to learn and master the concepts presented.

In the previous article From Basic to Intermediate: Variables (I)", we started talking about variables and some aspects related to them. For example, we discussed how to convert variables into constants. We also started to talk about the lifetime and visibility of variables.

Here we will continue this topic, based on the assumption that the reader has properly understood the previous material. One thing we find when talking about the variable lifetime and visibility can be a bit difficult for beginners to understand. The reason is that in many cases we don't want global variables to create inconvenience. We want variables to exist only within a single block of code. However - and this is where things get complicated - we don't want the variable's value to die or disappear after the block ends.

This situation is one of the most confusing in the minds of many programmers, including beginners who want to become professionals. This happens because many people don't realize that some programming languages have mechanisms that allow a variable to retain its value in memory. This complexity is likely due to the fact that popular scripting languages such as Python do not use this implementation. For this reason, it is very difficult for a programmer accustomed to Python to understand this concept. Variables do not always lose, or rather forget, their value when the block to which they belong ceases to exist.

Author: CODE X