dark mode light mode Search Menu
Search

Variables

Wonderlane on Flickr

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.

C

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.

Lua

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.

PHP

$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.

Python

width = 10

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

Rails

@@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

C

http://www.codingunit.com/c-tutorial-variables-and-constants
http://www.zentut.com/c-tutorial/c-variables/
http://tigcc.ticalc.org/doc/keywords.html

Lua

http://www.lua.org/pil/4.2.html (local variables)
http://www.lua.org/pil/1.2.html (global variables)
http://lua.gts-stolberg.de/en/Variablen.php
http://lua-users.org/wiki/ScopeTutorial
http://www.lua.org/pil/1.3.html (reserved words)

PHP

http://php.net/manual/en/language.variables.basics.php
http://php.net/manual/en/language.variables.php
http://www.w3schools.com/php/php_variables.asp
http://php.net/manual/en/reserved.php

Python

http://docs.python.org/3.3/tutorial/introduction.html
http://docs.python.org/3.3/tutorial/introduction.html#first-steps-towards-programming
http://docs.python.org/3.3/tutorial/classes.html#private-variables
http://docs.python.org/3/reference/lexical_analysis.html#keywords

Rails

http://www.tutorialspoint.com/ruby/ruby_variables.htm
http://www.wellho.net/mouth/2609_Scope-of-variables-important-to-Ruby-on-Rails.html
http://www.tutorialspoint.com/ruby/ruby_syntax.htm

Variables

http://www.cs.utah.edu/~germain/PPS/Topics/variables.html
https://en.wikipedia.org/wiki/Variable_%28computer_science%29

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

http://arstechnica.com/information-technology/2013/03/how-to-name-a-variable-when-the-word-is-both-a-noun-and-a-verb/