×

Difference between Definition and Declaration in Coding

Both are very confusing terms in the world of coding. These two concepts are different to each other. The term Definition is used in the memory assignment of a variable while the declaration memory is not allocated to any variable. 

What is Definition?

The definition is a term used to identify the code associated with class, variable, function etc. A compiler must be needed to use the definition. When a variable is defined, then the definition holds some memory. It provides the code for any function. The definition of a program is unique; that's why it can be used only once in a program. There are so many connections between declaration and definition. Sometimes we cannot define a variable in the program, but we can determine that variable in that program. 

For example, we cannot use the class definition, but we can declare it.

Definition Example:

  • Variable definition as well as the declaration:
int p = 11;
  • Function definition: 
nt add (int v, int k)
{
int g;
g = v + k;
return g;
}

What is Declaration?

We use the term declaration for specifying a name in the program like variable, class, function, variable and expression function etc. We cannot use any name without declaring it in the code. So first, we have to report the name in the code, and then we can use it in the code. We can declare an element many times in the code. We can also use the declaration statement to increase the visibility of the code. 

There are other reasons to use the definition statement instead of the declaration in the code. These are as the following-

  • When the declaration statement becomes a typed statement.
  • When the class name is declared without defining it.
  • When we want to declare an external link to the code.
  • When we want to declare a static variable inside the code, we first have to define that variable in the code; after that, we can claim that in the code.

Declaration Example

  • Declaration of variable:
extern int h;
  • Declaration of function:
int add (int a1, int a2);
DefinitionDeclaration
It is used to determine the value like class, variable etc., used in the code or program.It specifies the name, like class, variable in the program or code.          
Memory allocation occurs in the case of definition. int car(int ship)
{
// some code
return ship;
}
int shop;
This is a definition of variables and functions. The allocation of storage is present here.
The memory allocation does not take place in the case of declaration.
int more(int);
extern int shop;   
This is a declaration of variables and functions. This declaration is used for informing the compiler about the variable and function.
The statement cannot be declared in the code or program more than once.In the case of Declaration, Redeclaration is easily possible in the code or program.
Its duration is determined.Its visibility is specified.  
It is used where the identifier is implemented.It introduces an identifier, and then describes it.
The identifier is used to complete the linking process with the compiler in the code.
For example
int main()
{
extern int more;
printf("%d\n", more);
return 0;
To allow the identifier for reference, compiler needs to identify it.
For example
int main()
{
printf("%d\n", main);
return 0;
A definition can be used instead of declaration.But a declaration cannot use instead of definition.
A variable can be defined only once in the code. int main() { int more; int more; int more; return 0; }  A variable or a function can be declared any number of times. For example, int more; int main() { extern int more; extern int more; extern int more; printf("%d", more); return 0; }  
{
int more;
int more;
int more;
return 0;
}
int main()
{
extern int more;
extern int more;
extern int more;
printf("%d", more);
return 0;
}

Conclusion

A variable can be defined and declared in the case of C language. It is also possible to define and declare the function. The main difference between definition and declaration is a declaration of a variable indicates the compiler, the name and the type of the variable. In contrast, the definition of a variable tells the compiler where and how much storage to create for a variable.


Related Topics

Difference between SDN and NFV

1. Software Defined Networking (SDN) The aim of the SDN design is to enhance overall network performance, and by providing a dynamic and programmatically effective network setup, networks may become nimble...

3 minutes read.

Difference between Open-Source Software and Proprietary Software

The commercial software business is most terrified by the idea of open-source software, yet open-source has plenty of developers and programmers on its plate who are least intimidated by the...

3 minutes read.

Difference between Coaxial Cable and Twisted Pair Cable

What is Coaxial Cable? A type of guided medium used to carry out the transmission of high-frequency signals is called coaxial cable. The solid inner conductor of coaxial cables aids in...

3 minutes read.

Difference between Associative Mapping and Direct Mapping in Cache

This article gives the brief explanation about the Associative and Direct mapping techniques. We will get a brief understanding about associative and direct mapping after going through this article. What is...

3 minutes read.

Difference between EBS and EFS

The Amazon web service provides a storage management system. There are two types of the storage management system. These are AWS EFS (Elastic File System) and AWS EBS (Elastic Block...

4 minutes read.

List Of All Difference

Difference Between Longitude And Latitude Difference Between Bone And Cartilage Difference Between Real And Virtual Image Difference Between Physical Change And Chemical Change Difference Between India And Australia Difference Between Need And Want Difference Between Current...

3 minutes read.

Difference between Traditional and Modern Concepts of Marketing

Traditional Concepts of Marketing The conventional view of marketing places all its emphasis on products, with the goal of producing and selling more goods to increase profits. Profit is the focus...

3 minutes read.

Difference between PROM and EPROM

Generally, we know that ROM (Read-Only Memory) is a memory that cannot be changed. It is a non-volatile memory, meaning that these memory chips will not be affected when the...

3 minutes read.

Difference between Extender and Repeater

Nowadays, we all use Wi-Fi in our houses and offices. We can easily connect and use the internet without any wire connection between the gadgets and the router. So, the...

4 minutes read.

Difference between Longitude and Latitude

Difference between Longitude and Latitude Whenever we go to new place or destination and we don’t know the exact address, we simply put the name of the location on Google Map....

4 minutes read.

Difference between Bezier Curve and B-Spline Curve

Nowadays, we all see that the use of graphics is increased. Graphics is used in all the websites, applications and also games. Ever wondered how such smooth curves are created...

3 minutes read.

Difference between E-Commerce and E-Business with an Example

Nowadays, no one is interested in going to the market to buy and sell their products. Now people just place an order through the internet, and their product is directly...

3 minutes read.

Difference between Open-Source Software and Free Software

Most people think that the difference between "free software" and "open-source software" is just a philosophy or approach. According to the Open-Source Initiative, these phrases are interchangeable and have the...

4 minutes read.

Difference between ASCII and Unicode

Introduction In this article, we are going to learn about the differences between ASCII and Unicode. We are going to study briefly about these two topics and study about major differences...

3 minutes read.

Difference between Procedural and Declarative Knowledge

The information can be presented in a variety of forms to the computer's inference in order to address issues related to the rule. So, in this article, we'll talk about...

2 minutes read.

Difference between Syntax and Semantics

The phrases syntax and semantics are crucial in relation to every computer language. The syntax of a computer language refers to the list of phrases that are allowed in that...

3 minutes read.

Difference between Physical Change and Chemical Change

Difference between Physical Change and Chemical Change There is a famous quote by Heraclitus, “Change is the only Constant.” This entire universe is working on the single concept that is Change. From seed...

4 minutes read.

difference between Dependency and DevDependencies

Introduction There is a file named package.json present in every web project. It contains all the data needed for the project, which is also called metadata. All the dependencies needed for...

2 minutes read.

Difference between EIGRP and OSPF

Both EIGRP and OSPF are interior gateway routing protocols. The aim of both EIGRP and OSPF is to choose the best route to transfer information from source to destination with...

4 minutes read.

Difference between Metrics, KPI's and Critical results

Usually, people use these terms, metrics, KPI’s and key results interchangeably as if they all mean the same, but these are different terms. So, let's learn these with transparency. Metrics The word...

3 minutes read.