Declaration (computer programming)

In computer science and programming, a declaration is to define the dimension, identifiers, data types and other aspects of a variable or subroutine. By declaring the translator (compiler or interpreter) is made known to this variable or this subroutine; it is thus possible to use them in other places in the same source code.

Frequently the terms declaration and definition with the general meaning of "data / data structures / data types define / describe " used as synonyms. For examples, see " define your own data types " in, " defined variable" or " define a class " in class.

Depending on programming, however, these terms are also used in different ways. This also functions as in Haskell by declaration specified in the programming languages ​​C and C is called the 'Definition'. , Definition ' is understood as a special case of the declaration. For variables we speak of definition, if the translator generates code that has been reserved either static ( in the data segment ) or dynamically (at runtime ) of space for this variable. In subroutines is called definition if at this point the source code of the subroutine is given. The declaration of a subroutine without defining it is often referred to as a prototype.

When referencing a declared (not defined ) variable or subroutine checks the linker that the variable or subroutine has been defined elsewhere and linked references the definition.

Only after the declaration of a variable can be assigned an expression. In addition to the explicit declaration is available in some programming languages ​​(such as Fortran, BASIC, PL / I) but also the possibility of an implicit declaration of variables: in this case, the first occurrence of a variable to an automatic type mapping leads.


The following example in C to declare and define the variable x with the data type int

Int x; In the next example, the keyword does the extern that the variable y is only declared but not defined. The definition must take place at a different location in the same or a different source file.

Extern int y; The third example declares the subroutine example1 without defining it. It consists only of the so-called functional head ( function prototype, or signature of the subroutine ).

Int example1 (char * c); In the next example, the subprogram is declared and defined example2. In curly braces is called the body function or function body, which contains the source code of the subroutine.

Int example2 ( int a, int b )    {      return a * b;    } In the following code example in the Cobol language the variables a, table ' - a data structure of type' array ' - defined / declared. These are for the specific data format suitable instructions as well as a so-called ' index ' ( its content is set and controlled, usually within a loop) to address:

* In the data part of the program: ( with '*' incipient instructions / texts are considered comments ) 01 TAB_PRODUKT. ** The entire data structure:     02 PRODUCT OCCURS 100 * Data for 1 Product; the structure can occur 100 times        03 NUMBER PIC 9 (5) * Numeric 5 points        03 NAME PIC X ( 25). * Alphanumeric 25 posts        03 PRICE PIC 9 ( 5) V99 Comp - third * Numeric - packed 5 digits 2 decimals (4 bytes) * In the command part of the program: ** Sum of all existing products    IF NUMBER ( INDEX) means <> 0 * zero ' not used ' (in this example )       ADD PRICE ( INDEX) TO SUMME_PREIS    ELSE ... Links and literature

  • Declarations in C. Accessed on 17 October 2010 ( on ).
  • George Paul, Meike Hollatz, Dirk Jesko, Torsten mane: Fundamentals of computer science for engineers. Vieweg Teubner, 2003, ISBN 3519004283, pp. 87, 95ff.