×

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 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 Audit and Review

Audit Vs Review Business entrepreneurs look for different options to save their time and money when it comes to financial statements. Both the methods: Audit and Review, is used for the...

4 minutes read.

Difference between Persistent and Non-Persistent Connection

We all use the internet, and we usually search on the internet for different things. It looks so easy when we search anything on the internet. It is all right...

3 minutes read.

Difference between First Angle Projection and Third Angle Projection

We use the orthographic projection technique to represent a 3D object on a 2D plane. The orthographic projection system makes use of parallel lines to transcribe 2D views of 3D...

2 minutes read.

Difference between Pure Substances and Impure Substances

Many people are confused between pure and impure substances. So, in this article, we will explain about both of them. What are Pure Substances? A pure material has a predictable chemical makeup....

3 minutes read.

Difference between Software Engineering and System Engineering

The usage of the mobile and all type of applications are increased a lot and to meet the needs of the users the software’s are getting better day to day....

4 minutes read.

Difference between President and Prime Minister

The country's first citizen and head of state is the President. The Prime Minister, on the other hand, leads the country's government at the national level, together with the other...

4 minutes read.

Difference between Fedora and CentOS Operating Systems

We are going to study about the Fedora and CentOS briefly. The Fedora and CentOS are both closely related to the Red Hat. These both help in maintaining the Operating...

3 minutes read.

Difference between EPROM and EEPROM

EPROM and EEPROM are pretty similar because they have many similarities, such as they both can be erasable and reusable, and both have non-volatile memory, so it is tough to...

3 minutes read.

Difference between Optical Fiber and Coaxial Cable

What is Optical Fiber Cable? Data is sent utilizing light over optical fiber connections. Dr Narinder Singh Kapani showed how optical fiber might be used to convey pictures and created the...

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 Server-Side and Client-Side Scripting

There are two places where the scripts can be written, at the server end (back end) or at the client end (server end). The fact that server-side scripts are processed...

4 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 PLA and PAL

In computer engineering, the logic gates are used in different cases. The PLA and PAL are the programmable logic devices having different functionalities. These are mostly used in dealing with...

3 minutes read.

Difference between Fixed and Dynamic Channel Allocations

Radio resource management for wireless and cellular networks uses channel allocation strategies to provide bandwidth and communication channels to base stations, access points, and terminal devices.The two types of channel...

3 minutes read.

Difference between Guidance and Counseling

Guidance meaning Guidance is the direction or advice given to a person by an expert or certified individual in a particular department. In other words, guidance means the action where an...

3 minutes read.

Difference between TDMA, CDMA, and FDMA

These are all methods of exchanging and transmitting data. Because of their unique traits and features, they are particularly useful in a variety of contexts. In this article, we will...

3 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 MD5 and SHA1

MD5 and SHA1 are two hashing methods. Although MD5 is quick and easy to use, it does not offer enough security. Compared to MD5, SHA1 is more complicated but offers...

3 minutes read.

Difference between npm and yarn

Package managers like npm and yarn assist in managing a project's dependencies. A dependency is a component of code that a project needs to function properly. They are necessary because...

3 minutes read.