Declaration (computer science)
Encyclopedia
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 strongly-typed languages (such as C
) that require variables and their types to be specified with a declaration before use.
In the BCPL
family languages such as C++
and Java
, it can also specify the variable
's dimension
s to declare a scalar, array or matrix. In this family, "pure" declarations (announcing the existence and properties of the element, commonly called simply "declarations") and definitions (declarations that provide the actual implementation
in the case of functions, and initialization
in the case of variables) 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 redefining of functions.
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 that are not definitions, in C:
Here are some examples of definitions, again in C:
. 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 "
's "
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).
CLISP
(GNU CLISP 2.35)
C# (.NET Framework
)
C
(GNU GCC
3.4)
JavaScript
(Mozilla Firefox 1.0)
Standard ML
(Standard ML of New Jersey v110.55)
MUMPS
Objective Caml
3.08
Perl
5.8
PHP
5
Python
2.4
Ruby
1.8
VBScript
(WSH 5.6)
Identifier
An identifier is a name that identifies either a unique object or a unique class of objects, where the "object" or class may be an idea, physical [countable] object , or physical [noncountable] substance...
, type
Type system
A type system associates a type with each computed value. By examining the flow of these values, a type system attempts to ensure or prove that no type errors can occur...
, and other aspects of language elements such as variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
and functions
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
. It is used to announce the existence of the element to the compiler; this is important in many strongly-typed languages (such as C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
) that require variables and their types to be specified with a declaration before use.
In the BCPL
BCPL
BCPL is a procedural, imperative, and structured computer programming language designed by Martin Richards of the University of Cambridge in 1966.- Design :...
family languages such as C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
and Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
, it can also specify the variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
's dimension
Dimension
In physics and mathematics, the dimension of a space or object is informally defined as the minimum number of coordinates needed to specify any point within it. Thus a line has a dimension of one because only one coordinate is needed to specify a point on it...
s to declare a scalar, array or matrix. In this family, "pure" declarations (announcing the existence and properties of the element, commonly called simply "declarations") and definitions (declarations that provide the actual implementation
Implementation
Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.-Computer Science:...
in the case of functions, and initialization
Initialization (programming)
In computer programming, initialization is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on programming language, as well as type, storage class, etc., of an object to be initialized. Programming constructs which perform...
in the case of variables) 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
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
or Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
allow redefining of functions.
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 that are not definitions, in C:
Here are some examples of definitions, again in C:
Variables
In some programming languages an implicit declaration is provided the first time such a variable is encountered at compile timeCompile time
In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time.The operations performed at...
. 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
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
's "
use strict
" or Visual BasicVisual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
'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).
CLISP
CLISP
In computing, CLISP is an implementation of the programming language Common Lisp originally developed by Bruno Haible and Michael Stoll for the Atari ST...
(GNU CLISP 2.35)
*** - EVAL: variable X has no value
C# (.NET Framework
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
)
Undefined variable x
C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
(GNU GCC
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...
3.4)
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
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
(Mozilla Firefox 1.0)
Error: x is not defined
Source File: file:///c:/temp/foo.js
Standard ML
Standard ML
Standard ML is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of theorem provers.SML is a modern descendant of the ML...
(Standard ML of New Jersey v110.55)
val y = x;
stdIn:1.9 Error: unbound variable or constructor: x
MUMPS
MUMPS
MUMPS , or alternatively M, is a programming language created in the late 1960s, originally for use in the healthcare industry. It was designed for the production of multi-user database-driven applications...
Set Y = X
Objective Caml
Objective Caml
OCaml , originally known as Objective Caml, is the main implementation of the Caml programming language, created by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy and others in 1996...
3.08
Unbound value x
Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
5.8
(no error)
Global symbol "$x" requires explicit package name at foo.pl line 2.
Execution of foo.pl aborted due to compilation errors.
PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
5
(no error)
PHP Notice: Undefined variable: x in foo.php on line 3
Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
2.4
Traceback (most recent call last):
File "foo.py", line 1, in ?
x = y
NameError: name 'y' is not defined
Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
1.8
NameError: undefined local variable or method `x' for main:Object
from (irb):1
VBScript
VBScript
VBScript is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It is designed as a “lightweight” language with a fast interpreter for use in a wide variety of Microsoft environments...
(WSH 5.6)
(no error)
(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'