Definition and Declaration are very confusing terms if you are new to programming. The two concepts are different in some ways as the definition involve memory assignment to the variables while in declaration memory is not allocated. The declaration can be done more than one time, conversely, an entity can be defined exactly once in a program.
The definition is automatically a declaration in most of the scenario. Now let’s understand the difference between definition and declaration with the detailed comparison chart.
Content: Definition Vs Declaration
Comparison Chart
Basis for comparison | Definition | Declaration |
---|---|---|
Basic | Determines the value stored in variable, function or class. | Specifies the name and type of variable, function, class, etc. |
Memory allocation | Occurs | Does not take place. |
Repetition | Statements cannot be defined again if once it is already defined. | Redeclaration can be easily possible. |
Scope | Duration is determined | Visibility is specified |
Definition of Definition
Definition identifies the code or data associated with the name of the variable, function, class, etcetera. The definition is necessarily required by the compiler to allocate the storage space for the declared entity. When a variable is defined it holds an amount of memory consist of several bytes for that variable. A function definition produces code for the function. We can define a program element just once in a program because the definition is a unique specification of a program element. The relationship between declaration and definition can be one-to-many.
In some situations, a program element cannot be defined but declared, for example when a function is never invoked or its address is never used even if it is declared. Another example is that in which the class definition is not used while it must be declared.
Definition of Declaration
Declaration is used to specify the names to the program such as the name of a variable, function, namespace, classes, etc. No name can be used in a program without its declaration. The program elements can be declared multiple times, unlike definition. Multiple declarations can only be achieved when the different declarations are made using the identical format. Declaration is the medium of providing visibility to the program element in the perspective of compilers.
The declaration serves the purpose of definition, only in certain cases the condition is not implied which are given below.
- When the static data member is declared inside a class declaration, in that case, it is not a declaration. Because it generates only one copy for all objects of the class and static data members are the components of the objects of a provided class type.
- If a declaration is typedef statement.
- A variable is declared without initializer or function body but includes extern specifiers. It indicates that the definition could be for the other function and provides the name external linkage.
- The class name declaration without including definition such as class T;
Usually, declaration takes place in a scope. The scope decides the visibility of the name declared and the defined object duration.
Key Differences Between Definition and Declaration
- The definition of a program element determines the value associated with that element. On the other hand, the declaration of a program element specifies its name and type to the compiler.
- The definition of the program element reserves some amount of memory while declaration doesn’t involve memory allocation.
- A program element can be declared multiple times. Conversely, definition incorporates a unique specification with the name of the program element which could be distinguished by any code or data.
- Scope in declaration describes the visibility of the variable, function, object, class, enumeration, etc. In contrast, in the definition the scope relates to the duration.
Definition Example
- Variable definition as well as the declaration:
int r = 10;
- Function definition:
int add (int x , int y) { int a; a = x + y; return a; }
Declaration Example
- Variable declaration:
extern int r;
- Function declaration:
int add (int p1, int p2);
Conclusion
The declaration process is used to make the program element visible to the compiler, and it doesn’t require to allocate the memory. Inversely definition is a declaration that reserve storage, in simple words the compiler reserves the memory space for the declared entity.
Leave a Reply