Open In App

Difference between Definition and Declaration

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In programming, the terms declaration and definition are often used interchangeably, but they have distinct meanings and purposes. A declaration is a way of informing the program about the name and type of an entity it will handle while a definition allocate storage or defines where the implementation of the entity can be gotten from.

The below table illustrates the primary differences between the declaration and definition of a variable/function:

ParameterDefinitionDeclaration
PurposeAllocates memory and initializes a variable or provides a function's implementation.Specifies the name and type of a variable or function.
Memory AllocationAllocates memory.Does not allocate memory.
InitializationCan include initialization.Does not include initialization.
ScopeCreates an entity in a specific scope.Provides information about an entity's type.
Function ContextIncludes the function body (implementation).Includes only the function signature without the body.
Multiple OccurrencesCan only occur once for a variable or function in a file.Can occur multiple times in a file.
Compilation PhaseHappens during the compilation phase where memory is allocated.Happens during the syntax checking phase.
LinkingUsed by the linker to resolve references.Informs the compiler about the existence of a variable or function.
Header FilesNot typically included in header files.Commonly included in header files.
Syntax RequirementMust follow the complete syntax for initialization or function implementation.Requires only the type and name of variables or functions.
VisibilityMakes the variable or function visible and usable.Makes the variable or function known to the compiler, but not usable without definition.

What is a Declaration?

Declaration of a variable is for informing the compiler of the following information: name of the variable and the type of the value it will hold i.e., declaration gives details about the properties of a variable. Whereas, in the Definition of a variable, memory is allocated for the variable.

In C language definition and declaration for a variable takes place at the same time. i.e. there is no difference between declaration and definition. For example, consider the following statement,

int a;

Here, the information such as the variable name: a, and data type: int, is sent to the compiler which will be stored in the data structure known as the symbol table. Along with this, a memory of size 4 bytes(depending upon the type of compiler) will be allocated. Suppose, if we want to only declare variables and not define them i.e. we do not want to allocate memory, then the following declaration can be used

extern int a;

In this example, only the information about the variable name and type is sent and no memory allocation is done. The above information tells the compiler that the variable a is declared now while memory for it will be defined later in the same file or in a different file.

What is Definition?

Declaration of a function provides the compiler with the name of the function, the number and type of arguments it takes, and its return type. For example, consider the following code,

int add(int, int);

Here, a function named add is declared with 2 arguments of type int and return type int. Memory will not be allocated at this stage.

Definition of the function is used for allocating memory for the function. For example, consider the following function definition,

int add(int a, int b) { return (a+b); }

During this function definition, the memory for the function add will be allocated. A variable or a function can be declared any number of times but, it can be defined only once.

Conclusion

In conclusion, understanding and implementing definitions and declarations in programs is important for any programmer. The declaration makes an entity known to the compiler and also the kind of entity it is so that the compiler can locate any instance of it in the code. Hence a definition goes further in that it assigns memory or gives the implementation of that entity. It makes understanding how code should be organized, how dependencies have to be managed and where types, their instances and their linkages should be kept and recalled important for constructing scalable and maintainable software systems.


Next Article

Similar Reads