Declaration (programming)

from Wikipedia, the free encyclopedia

In computer science and programming , a declaration is the definition of dimension , identifier , data type and other aspects of a variable or a subroutine . The declaration makes this variable or subroutine known to the translator ( compiler or interpreter ); it is thus possible to use them in other places in the same source code .

The terms declaration and definition are often used as synonyms with the general meaning “data / data structures / data types define / describe”. For examples see "Define your own data types" in, "Defined variable" in or "Define a class" in class .

Depending on the programming language, however, these terms are sometimes used differently. In Haskell, for example, functions are also specified by declaration; in the C ++ and C programming languages ​​this is called a 'definition'. 'Definition' is understood as a special case of the declaration. In the case of variables, one speaks of definition when the translator generates code that reserves memory space for this variable either statically (in the data segment) or dynamically (at runtime). In the case of subroutines, the term definition is used if the source text of the subroutine is specified here. The declaration of a subroutine without a definition is often referred to as a prototype .

When referencing a declared (undefined) variable or subroutine, the linker checks that the variable or subroutine has been defined elsewhere and links the references with the definition.

Only after the declaration of a variable, an expression assigned to be. In addition to the explicit declaration, in some programming languages ​​(e.g. Fortran , BASIC , PL / I ) there is also the option of an implicit declaration of variables: In this case, the first occurrence of a variable leads to an automatic type assignment.


The following example in the C programming language declares and defines the variable x with the data type int .

  int x;

In the next example, the keyword extern has the effect that the variable y is only declared, but not defined. The definition must be made at a different location in the same or a different source text file.

  extern int y;

The third example declares the subroutine example1without defining it. It only consists of the so-called function head ( function prototype or signature of the subroutine).

  int example1(char *c);

In the next example the subroutine example2 is declared and defined. The so-called function body or function body, which contains the source text of the subroutine, is in curly brackets.

  int example2(int a, int b)
    return a * b;

In the following code example, the variables of a 'table' - a data structure of the 'array' type - are defined / declared in the COBOL programming language . These are to be addressed with instructions suitable for the respective data format as well as a so-called 'index' (the content of which is usually set and checked within a loop ):

* Im Datenteil des Programms:    (mit '*' beginnende Anweisungen/Texte gelten als Kommentare)
01 TAB_PRODUKT.                       ** Die gesamte Datenstruktur:
   02 PRODUKT    OCCURS 100.           * Daten für 1 Produkt; die Struktur kann 100 mal auftreten
      03 NUMMER    PIC 9(5)            * Numerisch 5 Stellen
      03 NAME      PIC X(25).          * Alphanumerisch 25 Stellen
      03 PREIS     PIC 9(5)V99 Comp-3. * Numerisch-gepackt 5 Stellen + 2 Dezimalstellen (4 Bytes)
* Im Befehlsteil des Programms:       ** Summieren des Preises aller vorhandenen Produkte
  IF NUMMER (INDEX) <> 0               * Null bedeutet (hier im Beispiel) 'nicht belegt'
  ELSE ...


  • Georg Paul, Meike Hollatz, Dirk Jesko, Torsten Mähne: Basics of computer science for engineers . Vieweg + Teubner, 2003, ISBN 3-519-00428-3 , pp. 87, 95 ff .

Web links

Individual evidence

  1. a b CGI programming with Haskell
  2. a b Basics of Computer Science ...  ( page no longer available , search in web archivesInfo: The link was automatically marked as defective. Please check the link according to the instructions and then remove this notice. (PDF; 117 kB) University of Magdeburg@1@ 2Template: Toter Link /