Home Segments Index Top Previous Next

129: Mainline

At this point, you are ready to learn about the distinction between a declaration and a definition. In general, a declaration is a program element that provides a compiler with essential information or useful advice. Thus, 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 definition, 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.

On the other hand, when you introduce a variable inside of a function definition, you only declare that variable, because the compiler does not set aside memory for such variables 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.