Declaration (computer programming)


In computer programming, a declaration is a language construct that specifies properties of an identifier: it declares what a word "means". Declarations are most commonly used for functions, variables, constants, and classes, but can also be used for other entities such as enumerations and type definitions. Beyond the name and the kind of entity, declarations typically specify the data type, or the type signature ; types may also include dimensions, such as for arrays. A declaration is used to announce the existence of the entity to the compiler; this is important in those strongly typed languages that require functions, variables, and constants, and their types to be specified with a declaration before use, and is used in forward declaration. The term "declaration" is frequently contrasted with the term "definition", but meaning and usage varies significantly between languages; see below.
Declarations are particularly prominent in languages in the ALGOL tradition, including the BCPL family, most prominently C and C++, and also Pascal. Java uses the term "declaration", though Java does not have separate declarations and definitions.

Declaration vs. definition

One basic dichotomy is whether or not a declaration contains a definition: for example, whether a declaration of a constant or variable specifies the value of the constant, or only its type; and similarly whether a declaration of a function specifies the body of the function, or only its type signature. Not all languages make this distinction: in many languages, declarations always include a definition, and may be referred to as either "declarations" or "definitions", depending on the language. However, these concepts are distinguished in languages that require declaration before use, and in languages where interface and implementation are separated: the interface contains declarations, the implementation contains definitions.
In informal usage, a "declaration" refers only to a pure declaration, while a "definition" refers to a declaration that includes a value or body. However, in formal usage, "declaration" includes both of these senses, with finer distinctions by language: in C and C++, a declaration of a function that does not include a body is called a function prototype, while a declaration of a function that does include a body is called a "function definition". By contrast in Java declarations always include the body, and the word "definition" has no technical meaning in Java.

Declarations and Definitions

In the C-family of programming languages, declarations are often collected into header files, which are included in other source files that reference and use these declarations, but don't have access to the definition. The information in the header file provides the interface between code that uses the declaration and that which defines it, a form of information hiding. A declaration is often used in order to access functions or variables defined in different source files, or in a library. A mismatch between the definition type and the declaration type generates a compiler error.
For variables, definitions assign values to an area of memory that was reserved during the declaration phase. For functions, definitions supply the function body. While a variable or function may be declared many times, it is typically defined once.
Dynamic languages such as JavaScript or Python generally allow functions to be redefined, that is, re-bound; a function is a variable much like any other, with a name and a value.
Here are some examples of declarations that are not definitions, in C:

extern char example1;
extern int example2;
void example3;

Here are some examples of declarations that are definitions, again in C:

char example1; /* Outside of a function definition it will be initialized to zero. */
int example2 = 5;
void example3

Undefined variables

In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time. In other languages, such a usage is considered to be an error, which may resulting in a diagnostic message. Some languages have started out with the implicit declaration behavior, but as they matured they provided an option to disable it.