Difference between Declaration and DefinitionIf you are a beginner to programming, the definition and declaration of the term might be highly confusing. In some aspects, the two concepts differ because the definition includes memory assignment to variables, whereas the declaration does not assign. The declaration may be made multiple times. However, an entity can only be defined once in a program. In this article, you will learn about the difference between Declaration and Definition. But before discussing the differences, you must know about Declaration and Definition with their examples. What is Declaration?Declarations are used to give program names, including variables, namespaces, functions, and classes. A name may not be utilized in a program until it is declared. In contrast to the definition, program items can be declared many times. Multiple declarations are only possible if the different declarations are made in the same format. In the view of compilers, the declaration is the means of making a program element visible. The declaration fulfils the aim of the definition, except in the following circumstances where the condition is not inferred.
Typically, a declaration occurs within a scope, and the scope determines the visibility of the stated name and the defined object's duration. Examples of DeclarationThere are various examples of declarations. Some examples are as follows: 1. Variable Declaration For example:
int a;
In the above example, the information containing the variable name: a and data type: int, are provided to the compiler and saved in the data structure referred to as the symbol table. Additionally, a memory block with a size of 2 bytes (depending on the compiler type) will be assigned. 2. Function Declaration For example:
int add(int a, int b);
Here, the function add is declared with two int-type arguments and an int-type return value. At this point, memory will not be allocated. What is the Definition?The definition identifies the code or data connected with the function, class, variable, etc. The compiler requires the definition to allocate storage space for the defined objects. When a variable is defined, it takes up memory in the form of multiple bytes. A function definition generates code for the function. You may only define a program element once in a program since the definition is a specific specification of a program element. The link between declaration and definition might be one-to-many. In some cases, a program element may not be defined but must be declared, such as when a function is never called, or its address is never used despite being declared. Another example is that the class definition is not utilized even if it must be declared. Examples of Definition1. Variable Definition For example:
int a = 10;
Here, the information has an integer value containing the value of 10. 2. Function Definition For Example:
int add (int a, int b)
{
int z;
z = a + b;
return z;
}
Here, the memory would be assigned the add functions during this function definition. Although a variable or function may be declared numerous times, it may only be defined once. Key differences between Declaration and Definition![]() There are various key differences between Declaration and Definition. Some of the key differences between the Declaration and the Definition are as follows:
Head-to-head comparison between Declaration and DefinitionHere, you will learn the head-to-head comparisons between the Declaration and Definition. The main differences between the Declaration and the Definition are as follows:
ConclusionThe declaration process makes the program element visible to the compiler and does not necessitate memory allocation. In contrast, the definition is a declaration that reserves storage. In other words, the compiler reserves memory space for the defined item.
Next TopicDifference between
|