World Library  
Flag as Inappropriate
Email this Article

Declaration (computer programming)

Article Id: WHEBN0004329406
Reproduction Date:

Title: Declaration (computer programming)  
Author: World Heritage Encyclopedia
Language: English
Subject: Initialization (programming), Scope (computer science), C (programming language), List of Latin words with English derivatives
Publisher: World Heritage Encyclopedia

Declaration (computer programming)

In computer programming, a declaration specifies properties of an identifier: it declares what a word (identifier) means.[1] 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.[1] Beyond the name (the identifier itself) and the kind of entity (function, variable, etc.), declarations typically specify the data type (for variables and constants), or the type signature (for functions); 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.[2] The term "declaration" is frequently contrasted with the term "definition",[1] 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

A basic dichotomy is whether a declaration contains a definition or not: for example, whether a declaration of a constant or variable specifies the value of the constant (respectively, initial value of a variable), 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.[1] 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.[2]

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 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 made in header files, which are included in other source files that reference and use these declarations, but don't have access to the definition. This creates an interface between the declaration and implementation, thereby increasing code modularity.[4] 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 redefining of functions.

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;
int example2 = 5;
void example3(void)


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 a fatal error, resulting in a diagnostic being issued. 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").

Examples of how various programming language implementations respond to undefined variables are given below. Each code example is followed by an error message (if any).


(setf y x)
*** - EVAL: variable X has no value

C# (.NET Framework)

static void Main()
  int y = x;
The name 'x' does not exist in the current context

C (GNU GCC 3.4)

int main()
  int y = x;
  return 0;
foo.c: In function `main':
foo.c:2: error: `x' undeclared (first use in this function)
foo.c:2: error: (Each undeclared identifier is reported only once
foo.c:2: error: for each function it appears in.)

JavaScript (Mozilla Firefox 1.0)

  y = x
Error: x is not defined
Source File: file:///c:/temp/foo.js

Standard ML (Standard ML of New Jersey v110.55)

val y = x;
stdIn:1.9 Error: unbound variable or constructor: x


Set Y = X

OCaml 3.08

let y = x;;
Unbound value x

Perl 5.8

my $y = $x;

(no error)

use strict;
my $y = $x;
Global symbol "$x" requires explicit package name at line 2.
Execution of aborted due to compilation errors.


$y = $x;

(no error)

$y = $x;
PHP Notice:  Undefined variable: x in foo.php on line 3

Python 2.4

 >>> x = y
 Traceback (most recent call last):
   File "", line 1, in ?
     x = y
 NameError: name 'y' is not defined

Ruby 1.8

irb(main):001:0> y = x
NameError: undefined local variable or method `x' for main:Object
        from (irb):1
        from /usr/bin/irb:12:in `

Real Basic / Xojo

Dim y As Integer
y = x
Real Basic Error:  Undefined method or property: x on line 2

VBScript (WSH 5.6)

Dim y
y = x

(no error)

Option Explicit

Dim y
y = x
(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'

See also


  1. ^ For example, Java uses "declaration" (class declaration, method declaration), while Python uses "definition" (class definition, function definition).[3]
  2. ^ This distinction is observed in Pascal "units" (modules), and in conventional C and C++ code organization, which has header files consisting largely of pure declarations, and source files consisting of definitions, though this is not always strictly observed, nor enforced by the language.


  1. ^ 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."
    C11 specification, 6.7: Declarations, paragraph 5.
  2. ^ 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[...]. 
  3. ^ 7. Compound statements, The Python Language Reference
  4. ^ Torsten Seemann. "Code Modularity and the C Programming Language." Monash University. Retrieved 2014-02-01. "

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."
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.