In their original specification of Pascal1, Niklaus Wirth and Kathleen Jensen appear to have glossed over the major syntactic change from Algol 60, which is stated as a primary inspiration. var is the only keyword of the language that does not have its own entry in the index. I can only guess that when he designed the language, Wirth wished to group declarations by their semantic categories, and also wanted a measure of parallelism in the syntax of those declarations.
At any rate, var suffers from a mild case of semantic overloading (although it was probably not recognized as such at the time):
- variable declarations
var is used to begin a block of variable declarations. As you might expect, "variables" are areas of memory that can be assigned values. All of the variables in the original version of Pascal would have been what C calls "automatic variables", existing within the scope of a procedure. Of course, as languages such as MODULA (and later versions of Pascal which incorporated ideas from MODULA such as ANSI Standard Pascal, as well as Borland derivatives of Pascal such as Turbo Pascal and Delphi) came along, it was sensible to use the same syntax for variables with global scope as well. Since Wirth's wonderful syntax diagrams don't convert to ASCII that well, you will have to be satisfied with a BNF syntax:
<variable-declaration-section> ::= var <variable-declarations>
<variable-declarations> ::= <typed-variable-declarations>
| <variable-declarations> <typed-variable-declarations>
<typed-variable-declarations> ::= <identifiers> : <type> ;
- call by reference parameters
var can also appear in the formal parameter list of a Pascal procedure or function.
<procedure-heading> ::= procedure <identifier> ;
| procedure <identifier> ( <formal-parameter-list> ) ;
<function-heading> ::= function <identifier> : <type> ;
| function <identifier> ( <formal-parameter-list> ) : <type> ;
<formal-parameter-list> ::= <formal-parameter-section>
| <formal-parameter-list> ; <formal-parameter-section>
<formal-parameter-section> ::= <identifiers> : <type>
| var <identifiers> : <type>
When it appears in a formal parameter section, var indicates that the formal parameters declared by the <identifiers> following the var are call by reference rather than call by value. That is, the memory area used for any actual parameter in a call to the procedure or function is that of a variable declared in (or a temporary created for) an enclosing procedure or function, rather than a temporary area created for that particular call. Any change to a 'var parameter', as it is called, is really a change to the outside variable.
Forgetting to add a var (or adding it where it shouldn't have been) to a formal parameter list was proably the number one source of bugs for people taking introductory Pascal courses, especially those who had first learned an all-call-by-reference language like FORTRAN.
1Jensen, Kathleen and Wirth, Niklaus. PASCAL: User Manual and Report. © 1974, Springer-Verlag Corrected Printing, 1978. ISBN 0-387-90144-2
2McCarthy, John. 2"Recursive Functions of Symbolic Expressions and their Computation by Machine" Communications of the ACM, April, 1960