Naming variables

You can give variables any name you want, such as the name of your dog or variations of four-letter words. However, it’s a good idea to use descriptive names that help identify the type of data that the variable holds, such as FirstName or CustomerIDNumber.

Remember Each variable must have a unique name to keep your computer from getting confused. In some languages, the variable names LastName, lastname, and Lastname are considered identical. Other languages, such as C++, are case-sensitive. A case-sensitive language considers a variable called LastName to be completely different from another variable called lastName. Unless every letter in a variable name is identical (including upper- and lowercase letters), a case-sensitive language considers variables to be two completely different items.

Warning! When naming variables in a case-sensitive language like C++, a common error is to name a variable something like CustomerAge but try to store data in that variable as customerAge. (Note that the first letter is lowercase rather than uppercase.) When naming variables, be consistent. Some programmers always capitalize the first letter of a variable name (Customer), but others always type the first letter as lowercase (customer). Some programmers always capitalize the first letter of each word (MyStudentNumber), but others don’t (Mystudentnumber). Whatever you do, do it consistently to save yourself a lot of headaches trying to identify your variables later on.

In some programming languages, you can name your variables and stuff data in them at the same time, as in the following Liberty BASIC program that creates variables. One variable (Salary) stores data that represents a user’s salary, and the second variable (TaxOwed) stores a calculation.


Salary = 25000                                    ?1
TaxOwed = Salary * 0.95                           ?2
PRINT “This is how much tax you owe = $”; TaxOwed ?3
END                                               ?4

This is how the Liberty BASIC program works:

  • ?1 Tells the computer, “Create a variable named Salary and store the number 25000 in that variable.”

  • ?2 Tells the computer, “Create a variable called TaxOwed. Then multiply the number stored in the Salary variable by the number 0.95 and store the result of this calculation in TaxOwed variable.”

  • ?3 Prints This is how much tax you owe = $ followed by the number that the variable TaxOwed represents.

  • ?4 Tells the computer that the program has ended.

In most programming languages, you use the equal sign (=) to assign a value to a variable. However, in Revolution, you use the magic into command instead, as follows:


put 25000 into Salary                             ?1
put Salary * 0.95 into TaxOwed                    ?2
put “This is how much tax you owe = $” && TaxOwed
           into message                           ?3

This is how the Revolution program works:

  • ?1 Tells the computer, “Put the number 25000 into a variable named Salary.”

  • ?2 Tells the computer, “Multiply the number stored in the Salary variable by the number 0.95 and put the result of this calculation in a variable named TaxOwed.”

  • ?3 Tells the computer, “Take the text, This is how much tax you owe = $ and include the number stored inside the TaxOwed variable and put all of this text in the message box that appears on-screen.” (The && symbol tells the computer to “add” the number stored in the TaxOwed variable to the end of the This is how much tax you owe = $ line.)


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

One Response to “Naming variables”

  1. I found your site on technorati and read a few of your other posts. Keep up the good work. I just added your RSS feed to my Google News Reader. Looking forward to reading more from you.

    Jason Rakowski

Leave a Reply



eXTReMe Tracker