Declaration (computer programming): Difference between revisions
m typo |
m WP:CHECKWIKI error fix for #61. Punctuation goes before References. Do general fixes if a problem exists. - using AWB (9916) |
||
Line 13: | Line 13: | ||
== Declarations and Definitions == |
== Declarations and Definitions == |
||
In the C-family of programming languages, declarations are made in [[ |
In the C-family of programming languages, declarations are made in [[header file]]s, 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.<ref>Torsten Seemann. "Code Modularity and the C Programming Language." http://www.csse.monash.edu.au/courseware/cse2304/hndtC.html: Monash University. Retrieved 2014-02-01. "</ref> A declaration is often used in order to access functions or variables defined in different source files, or in a [[Library (computing)|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 (programming language)|Python]] generally allow redefining of functions. |
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 (programming language)|Python]] generally allow redefining of functions. |
||
Line 45: | Line 45: | ||
{{samp|*** - EVAL: variable X has no value}} |
{{samp|*** - EVAL: variable X has no value}} |
||
[[C Sharp (programming language)|C#]] ([[.NET Framework]]) |
[[C Sharp (programming language)|C#]] ([[.NET Framework]]) |
||
Line 56: | Line 55: | ||
{{samp|Undefined variable x}} |
{{samp|Undefined variable x}} |
||
[[C (programming language)|C]] (GNU [[GNU Compiler Collection|GCC]] 3.4) |
[[C (programming language)|C]] (GNU [[GNU Compiler Collection|GCC]] 3.4) |
||
Line 71: | Line 69: | ||
{{samp|foo.c:2: error: (Each undeclared identifier is reported only once}} |
{{samp|foo.c:2: error: (Each undeclared identifier is reported only once}} |
||
{{samp|foo.c:2: error: for each function it appears in.)}} |
{{samp|foo.c:2: error: for each function it appears in.)}} |
||
[[JavaScript]] (Mozilla [[Firefox]] 1.0) |
[[JavaScript]] (Mozilla [[Firefox]] 1.0) |
||
Line 80: | Line 77: | ||
{{samp|Error: x is not defined}} |
{{samp|Error: x is not defined}} |
||
{{samp|Source File: file:///c:/temp/foo.js}} |
{{samp|Source File: file:///c:/temp/foo.js}} |
||
[[Standard ML]] (Standard ML of New Jersey v110.55) |
[[Standard ML]] (Standard ML of New Jersey v110.55) |
||
Line 87: | Line 83: | ||
{{samp|stdIn:1.9 Error: unbound variable or constructor: x}} |
{{samp|stdIn:1.9 Error: unbound variable or constructor: x}} |
||
[[MUMPS]] |
[[MUMPS]] |
||
Line 94: | Line 89: | ||
{{samp|<UNDEF>}} |
{{samp|<UNDEF>}} |
||
[[OCaml]] 3.08 |
[[OCaml]] 3.08 |
||
Line 102: | Line 96: | ||
{{samp|Unbound value x}} |
{{samp|Unbound value x}} |
||
[[Perl]] 5.8 |
[[Perl]] 5.8 |
||
Line 118: | Line 111: | ||
{{samp|Global symbol "$x" requires explicit package name at foo.pl line 2.}} |
{{samp|Global symbol "$x" requires explicit package name at foo.pl line 2.}} |
||
{{samp|Execution of foo.pl aborted due to compilation errors.}} |
{{samp|Execution of foo.pl aborted due to compilation errors.}} |
||
[[PHP]] 5 |
[[PHP]] 5 |
Revision as of 02:06, 7 February 2014
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 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, and is used in forward declaration.[1]
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, "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 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.[2] 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)
{
int x = 7;
}
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 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).
CLISP (GNU CLISP 2.35)
(setf y x)
*** - EVAL: variable X has no value
static void Main()
{
int y = x;
}
Undefined variable x
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
<UNDEF>
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 foo.pl line 2. Execution of foo.pl aborted due to compilation errors.
PHP 5
$y = $x;
(no error)
error_reporting(E_ALL);
$y = $x;
PHP Notice: Undefined variable: x in foo.php on line 3
Python 2.4
>>> x = y
Traceback (most recent call last):
File "foo.py", 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 `<main>'
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
References
- ^ Mike Banahan. "2.5. Declaration of variables". http://publications.gbdirect.co.uk/c_book/: GBdirect. Retrieved 2011-06-08.
[A] declaration [...] introduces just the name and type of something but allocates no storage[...].
{{cite web}}
: External link in
(help)|location=
- ^ Torsten Seemann. "Code Modularity and the C Programming Language." http://www.csse.monash.edu.au/courseware/cse2304/hndtC.html: Monash University. Retrieved 2014-02-01. "