Difference between Definition and Declaration
Last Updated :
23 Dec, 2024
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:
Parameter | Definition | Declaration |
---|
Purpose | Allocates memory and initializes a variable or provides a function's implementation. | Specifies the name and type of a variable or function. |
---|
Memory Allocation | Allocates memory. | Does not allocate memory. |
---|
Initialization | Can include initialization. | Does not include initialization. |
---|
Scope | Creates an entity in a specific scope. | Provides information about an entity's type. |
---|
Function Context | Includes the function body (implementation). | Includes only the function signature without the body. |
---|
Multiple Occurrences | Can only occur once for a variable or function in a file. | Can occur multiple times in a file. |
---|
Compilation Phase | Happens during the compilation phase where memory is allocated. | Happens during the syntax checking phase. |
---|
Linking | Used by the linker to resolve references. | Informs the compiler about the existence of a variable or function. |
---|
Header Files | Not typically included in header files. | Commonly included in header files. |
---|
Syntax Requirement | Must follow the complete syntax for initialization or function implementation. | Requires only the type and name of variables or functions. |
---|
Visibility | Makes 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.
Similar Reads
Difference between Document Type Definition (DTD) and XML Schema Definition (XSD) Document Type Definition and XML Schema Definition both contain a set of rules that control the structure and elements of XML files, but they are different in syntax and usage. They check whether an XML document has a valid structure or not. This article will discuss the difference between DTD and X
5 min read
Difference between declaration of user defined function inside main() and outside of main() The declaration of a user-defined function inside the main() function and outside main() is similar to the declaration of local and global variables, i.e. When we declare a function inside the main, it is in local scope to main() and that function can be used in main() and any function outside main(
4 min read
Function Declaration vs. Function Definition Function Declaration introduces the name, return type, and parameters of a function to the compiler, while Function Definition provides the actual implementation or body of the function. Declarations enable calling the function elsewhere in the code, while definitions provide the code to be executed
2 min read
Difference between âfunction declarationâ and âfunction expression' in JavaScript Functions in JavaScript allow us to carry out some set of actions, important decisions, or calculations and even make our website more interactive. In this article, we will learn the difference between âfunction declarationâ and âfunction expressionâ. The similarity is both use the keyword function
2 min read
Difference between Abstract and Concrete Data Structure In this article we will discuss the differences between Abstract and Concrete data structure or type. Abstract Data Types(ADT) : It is a type (or class) of objects whose behaviour is defined by a set of values and a set of operations. The user interacts with the interface, using the operations that
2 min read