Section 3.2 - Identifiers

Ada requires names for procedures, packages, and many other constructs. These names are called identifiers. Sample identifiers include ``Hello'', ``Launch_Torpedo'', and ``X12''. Identifiers must begin with a letter, though after that initial letter they may also contain digits and underscores. As noted in the last section, upper and lower case are considered equivalent.

Here is the required syntax for an identifier in BNF format:

identifier ::= letter { [ "_" ] letter_or_digit }
letter_or_digit ::= letter | digit

All characters of an identifier are significant, and Ada compilers must support lines and identifier lengths of at least 200 (!) characters. Hopefully you won't use that many, of course, but the idea is to be very flexible.

One implication of this syntax is that underscores must not be adjacent to each other. This was intentional, because on some printers two adjacent underscores look the same as one underscore. Underscores also can't begin or end an identifier.

You can use single letters as identifiers, but don't abuse this ability. If your program uses only single-letter identifiers it will be very difficult to decipher later. It's best to use identifiers that clearly state what they store or what they do. Also, while the Ada language permits the single letters "L" and "O" to be identifiers, I recommend against it - a lower case letter "L" is nearly indistinguishable from the digit one (1), and an upper case letter "O" is nearly indistinguishable from the digit zero (0) on some systems.


Quiz:

Here are some lists of identifiers:

  1. Hello, 2Run, Really_Quit
  2. Refresh_Screen, X22

Which of the preceding lists have only legal identifiers (ignoring the commas, which are there to separate the identifiers)?

  1. List 1
  2. List 2

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 3 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s3s2.htm".