Tuesday, August 31, 2010

Variables On Programming Language

When writing in machine code, machine addresses are used to specify where items are to be stored. The programmer has to keep track of what type of object a storage cell contains. Extending this somewhat, a variable provides an abstarction for this notion. A we shall describe shortly, a variable is bound to a tuple7 of attributes: (name, address, type, value). Other important concepts include the scope and the lifetime of the variable, as well as issues of the time of binding, scoping rules, and type checking.

Identifiers

Identifiers or names are not used for variables only. In a program, names may be as signed to such things as procedires,labels,types, and more.
While early languages permitted only single characters as names, most ALGOL like languages permit some string of letters and digits. The string starts with a letter to prevent syntatic confusion, as, for example, between a name like 10x and the integer 10. Names in COBOL, early versions of FORTRAN (through FORTRAN-77), and PL/I were restirected to uppercase letters, but mixed case is normal for many languages. One must be careful to check language rules, however. For example, a FORTRAN-90 compiler may recognize lowercase letters, but is not required to, so continued use of uppercase is common.


Language may place limits on the lengths of names or on the number of significant characters. In early C compilers, only the first eight were significant, so that dataQueue and dataQueue2 were not distinguished. ANSI C now specifies the first 31 to be significant. While some language specigications permit any name length, an implementation may force limitions.
Language like C abd Ada permit the use of the underscore character as well, and LISP permits the hyphen. Since a program can be more readble with meaningful names, multiple word identifiers are encouraged. In Pascal, one might mix cases to use name like dataQueue, while Ada programmers might use data_queue. When names are not case sensitive, then DataQueue. dataqueue, dataQueue, and DATAQUEUE would all rever to the same variable.

While style conventions for a programming language may be set by common usage, programmers are often guided by the standard reference manuals. In the Ada 83 standard, for example, identifiers were listed in uppercase (such as DATA_QUEUE), while the Ada 95 standard would use Data_Queue. As a result, books are starting to shift to this new style.

In C, however, names are case sensitive, so one must be careful with naming and perhaps adopt a convention of using only lowercase identifiers for variables and names beginning with an uppercase letter for procedures and function. Any variation from the convention can cause bugs in programs. In Java, the convention is to start the names of classes with uppercase letters, while other identifiers start with lowercase, e.g., dataQueue. Normal practice for other languages vary, so it is important to check naming conventions when learning a new language.

Reserved Words and Keywords 


Many languages use certain names as part of their syntax (such as for, while, of, else, end, etc.) or as special functions or operators (mod, nil, not, sin, and input/output routines like read or print). Any word whose meaning is predefined and cannot be redefined by the programmer is called a reserved word. When starting with a new language, it is not uncommon for a novice programmer to unintentionally use one of the reserved words as a variable name. Hopefully the compiler will recognize this error as a simple one and produce a clear error message. If the error  message is misleading, the problem could be rather trciky for a novice to diagnose.

There are often a number of words which are not reserved but have a predefined meaning. These keywords may, in fact, be defined by the user for another meaning. In Pascal, for example, most of the prefined types (integer, real, boolean, etc). and predefined functions (trunc, sqrt, sin, ln, etc). are not reserved. In Ada, a number of such items are provided in the Standard package. If one does use a name like integer for a variable, however, then the predefined meaning  may be unavailable, and the program may be more difficult to read. The same problem can arise in FORTRAN, in which no words are reserved.

No comments:

Post a Comment