Difference between Declaration and Definition

If 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.

  1. When a static data member is declared within a class declaration, it is not a declaration. As it only creates one copy of each object in the class, and static data members are the elements of objects of a given class type.
  2. When the declaration contains a typedef statement.
  3. A variable is declared without an initialize or a function body, but it contains extern specifies. It specifies the definition for the other function and gives the label external linkage.
  4. When the declaration of a class happens without the inclusion of its definition (like the class T).

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 Declaration

There 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 Definition

1. 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

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:

  1. The value associated with a program element is determined by its definition. In contrast, the program element's declaration defines its name and type to the compiler.
  2. A program element may be declared several times. In contrast, the definition includes a specific specification with the name of the program element, which any code or data can identify.
  3. The scope of a variable, function, object, class or enumeration in a declaration describes its visibility. In contrast, the scope in the definition refers to the time.
  4. The program element's definition reserves some memory. In contrast, the declaration does not include memory allocation.
  5. Once an entity has been defined, the definition procedure cannot be repeated indefinitely. In contrast, redeclaration is always feasible at any given time, even if you define an entity.

Head-to-head comparison between Declaration and Definition

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

FeaturesDeclarationDefinition
DefinitionDeclarations are used to give program names, including functions, variables, namespaces, and classes.The definition identifies the code or data connected with the function, class, variable, etc.
Variable or FunctionThe variable or function may be declared multiple times.The variable or function can be defined only once.
Memory AllocationIt doesn't allocate memory to the entities.It allocates memory to an entity.
ScopeThe scope of a variable, function, object, class or enumeration in a declaration describes its visibility.The scope in the definition refers to the time.
RepetitionOnce an entity has been defined, the definition procedure cannot be repeated indefinitely.Even if you define an entity, redeclaration is always feasible at any given time.

Conclusion

The 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.






Latest Courses