Home Segments Index Top Previous Next

129: Mainline

In general, a declaration is a program element that provides a compiler with essential information or useful advice. In C++ programming, the word declaration is most often used for program elements that inform the compiler about the data type associated with an identifier. For example, when you specify the type of a variable or parameter, you are said to declare the variable or parameter.

A definition causes a compiler to set aside memory at compile time. For example, when you introduce a variable outside of any function body, you both declare and define the variable, because you inform the compiler about the variable's type and you cause the compiler to set aside memory for the variable at compile time.

When you introduce a variable inside of a function body, you only declare that variable, because the compiler does not set aside memory for that variable at compile time.

Generally, when a variable is both declared and defined, you say, as a sort of shorthand, that it is defined; otherwise, it is declared.

Functions are both declared and defined, because you must specify their return type and because the compiler sets aside memory for functions at compile time.