Foundation Of Programming: Variables
Dive into variables and understand why they are so important
Anyone who has done a little bit of programming has probably heard of variables. It is one of the fundamental concepts of programming and it is what makes programming generally worthwhile. Let's have a look.
What Is A Variable?
A variable is a memory location on your computer that has been given a name. This name is given by the programmer when writing code. Memory locations usually have very cryptic addresses which is why programmers thought it would be nice to label memory locations with human-readable names that would help us easily located the memory location holding the data our code needs.
How Are They Used?
Besides providing an easy name for accessing data stored in memory, variables are one of (if not) the most used feature of programming. Variables can hold different values at different points in time, hence their name. This is useful because this provides the ability for the same code to work for many different situations. A restaurant billing program would use the same code to produce different bills for different customers because the program variables can hold different values depending on the input by the program users.
Declaring A Variable
To be able to use a variable in our code, we need to declare them. Declaring a variable can be different for most programming languages. However, the main purpose for declaring variables is to tell the computer to label a memory location with your variable name and also to specify what type of data (number, text, etc) the memory location will hold. This is to help the computer perform the correct operations depending on the type of data it holds. Some modern programming languages do not explicitly require you to declare a variable's type but will figure out the type once you put data into the variable.
Putting data in a variable after declaring it is called "initializing a variable"
Most programming languages have a specific set of rules for naming variables. These rules may differ in number depending on the language but we'll look at some of the common rules that most programming languages would have for naming variables.
- No spaces: Variables must never have spaces in their name. If your variables name uses multiple words, there are different ways of writing it without using spaces. We'll look at that later
- No special characters: Most languages will not let you name variables with non-alpha-numeric characters except a few exceptions like _, $, -. Variable names are supposed to be easy to read so using these special characters defeats this purpose.
- No keywords: Every programming language has some words that you can not use for a variable name. For example, in most programming languages, you can't use the word "class" as a variable name because it is used to declare a programming construct called a class.
There are many other rules depending on the language you're using so it's important to be aware of these rules and follow them accordingly.
Variable Naming Conventions
Like I said earlier, there are different ways to write multi-word variable names without using spaces since that is illegal in programming. Some common naming conventions include.
- camelCasing: This is a naming conversion where the first word starts in lowercase and every subsequent word starts with a capital letter but NO SPACES! Eg. superLongVariableName.
- PascalCasing: Similar to camel casing but this time the first word is also capitalized. Eg MyPascalCaseVariable.
- snake_casing: In this convention, all words are lowercase but separated by underscores(_). eg. really_long_snake_case_variable.
That's it for this one, guys. Hopefully, you got some insight on variables, how they work, and how to create. In the coming articles, we'll explain more about them together with other programming concepts. If you enjoyed this please leave a like, I would really appreciate it. Thanks for reading and I will see you on the next one.