Declaration (computer science)

Declaration (computer science)

In programming languages, a declaration specifies the identifier, type, and other aspects of language elements such as variables and functions. It is used to announce the existence of the element to the compiler; this is important in many languages (such as C) which require variables to be declared before use.

In the BCPL family languages such as C++ and Java, it can also specify the variable's dimensions to declare a scalar, array or matrix. In this family, declarations (announcing the existence and properties of the element) and "definitions" (providing the actual implementation) can be made independent of each other. Declarations are generally made in header files, which are meant to be included in other files that reference and use these declarations but don't have access to the definition. If a definition type doesn't match the previous declaration for the same element, the situation 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 allow the redefinition of functions, though.

A declaration is often used in order to be able to access functions or variables defined in different source files, or in a library.

Here are some examples of declarations which are not definitions, in the language C:extern int example1;void example2(void);

Here are some examples of definitions, again in C:int example1 = 5;

void example2(void){ int x = 7;}

ee also

* Function prototype
* Scope

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Abstraction (computer science) — In computer science, abstraction is the process by which data and programs are defined with a representation similar to its pictorial meaning as rooted in the more complex realm of human life and language with their higher need of summarization… …   Wikipedia

  • AP Computer Science — Advanced Placement Computer Science (also called APCS) is the name of two distinct Advanced Placement courses and examinations offered by the College Board to high school students as an opportunity to earn college credit for a college level… …   Wikipedia

  • Assignment (computer science) — In computer programming, an assignment statement sets or re sets the value stored in the storage location(s) denoted by a variable name. In most imperative computer programming languages, assignment statements are one of the basic statements.… …   Wikipedia

  • Default (computer science) — A default, in computer science, refers to a setting or value automatically assigned to a software application, computer program or device, outside of user intervention. Such settings are also called presets, especially for electronic devices. The …   Wikipedia

  • Generator (computer science) — In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates …   Wikipedia

  • Parameter (computer science) — In computer programming, a parameter is a variable which takes on the meaning of a corresponding argument passed in a call to a subroutine. In the most common case, call by value, a parameter acts within the subroutine as a local (isolated) copy… …   Wikipedia

  • Constructor (computer science) — In object oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in… …   Wikipedia

  • Class (computer science) — In object oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.More technically, a class is a cohesive… …   Wikipedia

  • Destructor (computer science) — In object oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources which were acquired by the object along… …   Wikipedia

  • Covariance and contravariance (computer science) — Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations (such as parameters, generics, and return… …   Wikipedia