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 Component and Object
1. Component : A component is a collection of objects that furnish a set of offerings to different systems. They have many elements in frequent with objects.Components can also be run both locally or in a distributed fashion. Many examples of locally run components exist and are oftentimes used to s
2 min read
Difference between C++ and Go
C++ was developed by Bjarne Stroustrup at Bell Labs in 1979 as an extension of the C languageC++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ is a widely popular language a
2 min read
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 Constant and Literals
In this article, we are going to discuss the differences between Constant and Literals in a programming language with examples. ConstantsConstants are used to define values that remain fixed and cannot be changed during the execution of a program.In C++, they are typically declared using the const k
3 min read
Difference between Certified and Registered
Certified and Registered are terms often used in various contexts, such as professions, products, and services, to indicate different levels of authorization, qualification, or recognition. Certification typically signifies that an individual or entity has met specific standards set by a governing b
5 min read
Difference between Variables and Constant
An algebraic expression is a mathematical statement that involves variables, constants and operations like addition, subtraction, multiplication, division and exponentiation.Algebraic Expression is described using terms and operations on those terms. For instance, x + 3 can be expressed as "3 more t
4 min read
Difference Between Source Code and Object Code
Anyone needs to have the background knowledge that is required when studying computer programming, especially that of the difference between source code and object code. Such terms are used when describing the process of developing software, compiling it, and its execution. In this article, you will
6 min read
Difference between data type and data structure
Data Type A data type is the most basic and the most common classification of data. It is this through which the compiler gets to know the form or the type of information that will be used throughout the code. So basically data type is a type of information transmitted between the programmer and the
4 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
Difference between Product Development and Product Deployment
These days, businesses need to carefully handle the complex processes involved in launching new goods and managing their evolution. These are two interrelated but different things essential for achievement in business. It is important to plan out, execute, and thus deliver customer-oriented goods wh
5 min read