Declaration (computer programming)
This article may require cleanup to meet Wikipedia's quality standards. The specific problem is: Too many examples of unclear relevancy; not enough references; problematic mentions of header files and multiple declarations. (December 2013) |
In
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 require separate declarations and definitions.
Declaration vs. definition
One basic dichotomy is whether or not a declaration contains a definition: for example, whether a variable or constant declaration specifies its value, or only its type; and similarly whether a declaration of a function specifies the body (implementation) of the function, or only its type signature.[1] 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.[a] However, these concepts are distinguished in languages that require declaration before use (for which forward declarations are used), and in languages where interface and implementation are separated: the interface contains declarations, the implementation contains definitions.[b]
In informal usage, a "declaration" refers only to a pure declaration (types only, no value or body), while a "definition" refers to a declaration that includes a value or body. However, in formal usage (in language specifications), "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
Declarations and definitions
In the C-family of programming languages, declarations are often collected into
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 (in C++, this is known as the One Definition Rule or ODR).
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 (the definition).
Here are some examples of declarations that are not definitions, in C:
extern char example1;
extern int example2;
void example3(void);
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(void) { /* definition between braces */ }
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 result 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 (e.g. Perl's "use strict
" or Visual Basic's "Option Explicit
").
See also
- Function prototype
- Scope (programming)
Notes
References
- ^ a b c d "A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
- for an object [variable or constant], causes storage to be reserved for that object;
- for a function, includes the function body;
- for an enumeration constant, is the (only) declaration of the identifier;
- for a typedef name, is the first (or only) declaration of the identifier."
- ^ Mike Banahan. "2.5. Declaration of variables". GBdirect. Retrieved 2011-06-08.
[A] declaration [...] introduces just the name and type of something but allocates no storage[...].
- ^ 7. Compound statements, The Python Language Reference
External links
- Declare vs Define in C and C++, Alex Allain
- 8.2. Declarations, Definitions and Accessibility, The C Book, GBdirect
- Declarations and Definitions (C++), MSDN
- "Declarations tell the compiler that a program element or name exists. Definitions specify what code or data the name describes."