beanz Magazine


Wonderlane on Flickr

In most or all software programming languages, variables work like containers to hold numbers, phrases, or other important stuff used in several places in your code. Here's how they work in several common languages.

In software programming, variables are names used to hold one or more values. Instead of repeating these values in multiple places in your code, the variable holds the results of a calculation, database call, results of a database query, or other value. Instead of multiple calculations or the load of multiple database calls to retrieve the same data, the variable is stored in computer memory once and used wherever it is needed in your code.

For example, imagine you have this calculation assigned to a variable called ‘math’:

math = 1+1+1

Now imagine you need this calculation in 10 different places in your code. You could copy and paste ‘1+1+1’ in all 10 spots in your code, like this:

answer = 1+1+1

Or you could use the variable ‘math,’ like this:

answer = math

When you have to update the calculation, for example, changing it to ‘1x2x3,’ you only update the part to the right of the math variable:

math = 1x2x3

One change to the definition of a variable is much easier than searching for ‘1+1+1’ and replacing it with ‘1x2x3.’

How variables are used is the same or similar across programming languages. Typically the variable is on the left followed by an equal sign followed by the item or items being assigned. Assignment of values to variables happens either at the top of a script, for example, if the variable is used widely, or as close to where it is used as possible. This helps readability and maintenance of code.

Variable identifiers can be different across languages. Here are examples of how variables are handled with several different programming languages.


int width = 10;

With the C programming language, you can specify the data type for a variable, in this case, an integer (int). Lines of code in C also end with a semi-colon.


width = 10

With the Lua programming language, variable names and assignments are brutally simple. There is no semi-colon to mark the end of a line of code.


$width = 10;

Note the dollar sign before the the variable name width, as well as the semi-colon to end the line. If it is important or useful to specify the data type for the variable, typically a short prefix is added to the variable name, for example, $intWidth for an integer or $arrHouseData for a variable that contains an array, or library, of data.


width = 10

As with Lua, Python does not use a prefix like the $ in PHP or explicit line endings like C or PHP.


@@width = 10

The Rails language has a number of ways to express variables based on their location and use within code. The use of double @@ symbols, for example, indicates a class variable whose value disappears and is unavailable outside the class that contains the variable. The scope of class variables differs from other variables used in the language. If you don’t know, in programming a class is a block of code. Classes contain a number of different items that can be called and referenced in other parts of an application, rather like variables.


With variables, it's also important to note:

  • Variable names must begin with a letter, underscore, or non-number character. Each language has its own naming conventions to guide how variables are named.
  • Variables can have a global scope, available everywhere in an application, or local to a specific function or script.
  • Do not use a comma with numbers, only the period or point.
  • Every language has reserved words that cannot be used in variable names, for example, Date. Instead, you might name your date-related variables dte or $StartDate.

As you learn a language, you'll learn the nuances of how each language handles variables, how they're assigned a scope, if that's possible, and other details. All languages, however, use variables as an efficient way to store and reuse data in software applications.

If you’re wondering about the photo of the rock above this article with the word ‘foo’ carved into it, foo is a variable and phrase you see a lot in software programming examples, for many languages.

Learn More


Lua (local variables) (global variables) (reserved words)





How to Name a Variable When it is both a Noun and a Verb

Also In The September 2013 Issue

An Interview with Troy Hunt

Troy Hunt is a software architect and Microsoft Most Valued Professional (MVP) focusing on security concepts and process improvement in a Fortune 50 company. He's based in Australia.

1Password, LastPass, RoboForm

If you use a password you created that is less than eight characters, your password is vulnerable to hacking. Here are three ways to create and use secure passwords online.

How to Write Secure Code

Coding securely doesn't have to kill the joy of programming. In fact, learning how to code securely provides insights into languages and computing.

How to Code HTML Email

How to code an HTML email like the ones you open every day turns out to be an offbeat software coding challenge.

What is an SSL Certificate?

How to tell if a web page is secure is one of the most basic yet least obvious ways to protect your data online.

Where to Find Command Line Interface Software

One key computing skill is the ability to use command line interface (CLI) software to enter commands to control a computer. Here are some options.


Lua is a comparatively simple programming language used in a wide range of places, from digital TVs to video games to phone applications. It's also designed to be simple to use and lightweight.


Here is how three programming languages handle a common problem: how do you organize and keep track of useful data?

Linux Command List for Command Line Interfaces

Some of the most common commands you'll need for a command line interface (CLI), in a Linux command list.

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.

News Wire Stories for October 2013

Must read stories about computer science, software programming, and technology for September 2013.

Learn More Links for October 2013

Links from the bottom of all the October 2013 articles, collected in one place for you to print, share, or bookmark.