Declaration (computer Programming)

Declaration (computer Programming)

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 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 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:

extern char example1; extern int example2; void example3(void);

Here are some examples of definitions, again in C:

char example1; int example2 = 5; void example3(void) { int x = 7; }

Read more about Declaration (computer Programming):  Variables

Famous quotes containing the word declaration:

    The Declaration [of Independence] was not a protest against government, but against the excess of government. It prescribed the proper role of government, to secure the rights of individuals and to effect their safety and happiness. In modern society, no individual can do this alone. So government is not a necessary evil but a necessary good.
    Gerald R. Ford (b. 1913)