Difference Between Declaration and Definition

Declaration Vs. Definition: Know What is the Difference Between Declaration and Definition

If you are new to the world of programming, then these two terms- Declaration and Definition are very confusing. They both are somewhat different because, in definition, we assign memories to the variables. On the other hand, we do not allocate memory in the case of declaration. You can declare an entity more than once, but you can’t define it multiple times in a program. Definition automatically becomes a declaration in a majority of scenarios. Let us now dive deeper into the difference between definition and declaration in detail.

What is a Definition?

A definition basically identifies the data or code associated with the name of class, function, variable, etc. A compiler necessarily requires the definition for allocating the storage space for an entity that is declared. When we define a variable, it holds memory consisting of many bytes (for that particular variable). The function definition provides the code for any function. One can only define a program element in a given program once because the definition is a program element’s unique specification. There can be one or many relationships between declaration and definition.

In some situations, one cannot define a program element but can declare it. For instance, it happens in some cases when we don’t invoke a function or when we don’t use an address even if we declare it. One more example can be when we don’t use the class definition but ultimately declare it.

What is a Declaration?

We basically use declarations for specifying the particular names of a given program- like the name of a class, namespace, function, variable, etc. You cannot use any name in a program without declaring it. Unlike definition, one can easily declare the elements of a program multiple times. But you can only achieve multiple declarations by making these multiple declarations using an identical format. You can use the declaration as a medium to provide visibility to the elements of a program from a compiler’s perspective.

Declaration basically serves a definition’s purpose. But it does imply some conditions in certain cases, such as:

  • When the declaration becomes a typed statement.
  • When the class name declaration occurs without including its definition (like the class T).
  • We declare a variable without the function body or an initializer, but it includes the external specifiers. It means that the definition may be for another function- thus, it provides an external linkage name.
  • A declaration usually takes place in the scope. This scope defines the overall visibility of a declared name and the object duration (defined).
  • When declaring a static data member inside a class declaration- we do not consider it a declaration. It is because it produces a single copy for all the objects in a class. The static data members form the components of the various objects in a provided class type.

Difference Between Definition and Declaration

Parameters

Definition

Declaration

Basics It aims at determining the overall values stored in a class, a function, or a variable. It aims at specifying the name of any given class, function, variable, etc.
Allocation of Memory Definition allocates memory to an entity. A declaration does not allocate memory to the entities.
Repetition Once you define an entity, you cannot keep repeating the definition process again and again. Even if you declare an entity, the process of redeclaration can always be possible at any given instance.
Scope It is determined. It has well-defined visibility.

Keep learning and stay tuned to get the latest updates on GATE Exam along with Eligibility Criteria, GATE Application Form, Syllabus, GATE Cut off, Previous Year Question Papers, and more.

Leave a Comment

Your Mobile number and Email id will not be published. Required fields are marked *

*

*