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

1 and 0

Binary numbers, based on 1s and 0s, reflect the practical essence of computer hardware: electricity is either on or off. Learn how to write in binary numbers, and the (not so secret) code to transform English language letters into binary numbers and back again.

Simon Haughton Talks about Kids, Python, and Computer Science

Simon recently wrote a short ebook, A children's guide to Python programming, to teach kids ages 5-8 computer programming with Python..


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.

Local Coding and Technology Groups

Girls Who Code, CoderDojo, and other local groups are great places to learn how to program, meet people, and help others learn.

How to Build a Computer

Building your own computer is a great way to not only save money, and get more processing power, but also to learn about the less obvious parts of software programming.

News Wire Stories for September 2013

Interesting stories about computer science, software programming, and technology for the month of August 2013.

Online Coding Schools

There are plenty of places online to learn one or more software programming languages. Here's a short list with some guidelines to evaluate all your options.

Computer science is no more about computers than astronomy is about telescopes, biology about microscopes, or chemistry about beakers and test tubes. Science is not about tools. It is about how we use them, and what we find out when we do.

Learn More Links for September 2013

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

Claude Shannon

His work ties together two topics for this issue of the magazine: binary numbers and circuit design. Without Shannon, computers and computer science could have been very different.