Variables 101: From Your Little Known to the Unlimited Unknown
What’s On This Page
Introduction
Variables form an integral concept in programming. Why, you ask? Well, let’s begin with the things you know about the topic. I bet anytime you ever encountered the word, it had something connected to data. Yeah, that’s why you just couldn’t get away from it in Statistics.
Picture yourself in a chemistry lab with no containers. It would be a nightmare. Chemicals would be unidentifiable, uncontrollable and scattered. You wouldn’t know if you’re working with Sulphur or sugar. Chaos would reign supreme, and a lot could go wrong in there.
Similarly, in the world of programming, where data is the lifeblood of a computer, we would need containers to hold and organize the data. These data-holders are what we call variables. They play a pivotal role in crafting dynamic programs, enhancing code readability, optimizing efficiency and simplifying debugging. Without them, the essence of coding – the ability to process and transform data to perform meaningful tasks – would be lost and the world of computing left in a state of chaos and inefficiency.
Pro-tip:
Join us on a 14-week journey to master these 5 essential EE engineering programming languages:C, C++, Python, Matlab and Assembly. Click on the link below
https://bit.ly/3sTV8Up
You can follow this Programming series through the link here Programming / Coding
Declaring Variables
The fact that we say variables are essential does not mean they appear magically or by chance. To put variables to work, you need to declare them. Declaring is like telling the computer, “Hey, I’m going to need a container to hold some data of a particular type.” Think of it as reserving a jar out of the many found in the home, just to hold sugar. You put the jar, label or differentiate it from the rest. This way, you establish a clear identity for that jar, making it easy to locate and use when needed.
Similarly, declaring a variable in programming involves giving it a name and specifying the type of data it can hold. The various languages have specific rules on variable declaration. Those are special topics for another time, but, for now, you just know that the variable will need a name(label), a data type (the kind of data it can take) and an assignment operator for declaration.
Choosing the right name for variables is paramount. This is why some conventions are good to follow, even though they are not binding. I’ll run you through some of them.
Firstly, variable name should be descriptive. It must provide a clear indication of the purpose or content; generic or vague names only lead to confusion.
Secondly, it is advisable to stay consistent with your naming convention. For instance, if you decide to move with the “concatenate words and capitalize each word’s first letter” style (e.g., VariableName), let that run throughout your code. Others may choose to go with “underscore separate words style (e.g., variable_name). Whatever convention you select, apply it uniformly across the codebase.
Lastly, convention has it that you can begin a variable name with only letters or an underscore and, from there, you can use letters, digits or underscores to complete the name. Certain characters and reserved names/keywords cannot be used at any point. Since those exceptions depend on the languages, you’ll need to find them on your own.
Variable Types
Maybe you haven’t noticed, but I introduced you to the important parts of a variable: its name and the data type. We just discussed the naming part, so let’s turn to the other side. Defining the kind of data your container can hold is very crucial in coding because it helps the computer to brace itself for the what’s coming (in terms of memory space) and flag any illegal immigrant entry. You can compare it to stating whether your jar/box will hold solids, fluids, powders, pastes. Understand?
Some common data types in the world of programming include;
Integer (int): This data type represents whole number without any decimal point. It can be either positive or negative. For example, ‘5’, ‘-9’ and ‘0’ are all integers. Integers consume a fixed amount of memory depending on the specific language. For instance, a 32-bit integer uses 4 bytes of memory space, while a 64-bit integer uses 8 bytes. This shows an integer can represent a wide range of numbers within its allocated memory space.
Float (float) and Double (double): These data types represent numbers with decimal points. Double typically offers twice the precision of a float. For example, ‘3.14’ or ‘2.2715’ are float or double values. Floating-point numbers require more memory compared to integers. A float typically uses 4 bytes, while a double uses 8 bytes. This extra precision allows for a wider range of values, but it also means they consume more memory.
Character (char): This type holds a single character, such as a letter, digit or symbol. For example, ‘A’, ‘7’, ‘b’ and ‘$’ are all characters. It usually is represented using 1 byte of memory, allowing them to store a single character from the character set.
String (str): It is a sequence of characters and allows us to work with text in programming. For example, “Hi, Bro” is a string. The amount of memory used depends on the length of the string and the encoding used.
Boolean (bool): This data type represents a binary value, often denoted as ‘true’ or ‘false’. Booleans are essential for making decisions in programming, as they determine the flow of execution. They use 1 byte of memory, though they can be more compactly represented in some systems.
Array: This is a data structure that can hold multiple values of the same data type. It allows us to efficiently store and manipulate collections of data. An array allocates memory based on the number of elements and their data type. For example, an array of integers will use more memory than an array of characters with the same number of elements.
Object: It is a data type that contains both data (attributes) and behavior (methods or functions). Object allows for a more structured and modular approach to programming. The memory usage depends on the size and complexity of the object as space is allocated for both the attributes and methods/functions associated with the object.
Null and Undefined: These are special data types that represent absence of value or lack of assignment. Null is an intentional absence of any value, while undefined typically indicates that a variable has been declared but not yet assigned a value. They don’t have a fixed memory allocation, but take up space to represent the absence of a value.
Pointer: For more advanced memory management and manipulation, a pointer is used to store memory addresses. It may not itself consume much memory, but the data it points to can have a significant impact on memory usage.
Custom Data Types: In some languages, you can create your own custom data types. These enable you to encapsulate data and functionality into a single unit.
Assigning Values
Now that we’ve covered the parts of a variable, there’s one last thing to deal with. Think of values as the substances you actually put in the container. Therefore, assigning values in programming is associating piece of data with a variable. The association allows you to refer to that data using the variable name throughout your program.
Assignment is done using the operator called the assignment operator (denoted by ‘=’). Yeah I know. Most people tend to confuse it with ‘equal to’ as an arithmetic operator, which usually is denoted by ‘==’. The assignment operator takes the value on the right-hand side and stores it in the variable on the left-hand side.
For example, here’s a line in python.
x = 5
In this case, we’re using the assignment operator to store the value ‘10’ in the variable container ‘x’. Now, we can use ‘x’ in our program anytime we want to refer to ‘10’.
Conclusion
In this discussion, we covered key programming concepts. Variables act as containers for data, crucial for effective coding. Naming variables thoughtfully and understanding data types are essential practices for clear and efficient code. The assignment operator links the value to the variable, a fundamental operation in programming. Mastering these basics lays a solid foundation for writing effective code in any language.