Variable is an essential term in computer science. It is used to define a specific type of data. If you don’t know the difference between declaration and definition of a variable then this article will tell you everything. The concept of declaring variables is similar to defining variables. However, there are a lot of differences between them. Here I will try to explain the differences between the two types of variables.
Declaration and definition of the variable
- Declaration is when the variable is used for the first time. There will be no values assigned to it.
- Definition is when you assign the value to a variable.
- Declaration is useful for declaring variables that are used for the first time. If you declare variables when they are already defined then the code will become more complicated.
- Declarations are not mandatory but definitions are.
- Declarations can be used anywhere in the program but definitions can be used only inside functions.
- Declarations are global while definitions are local.
- You can declare variables at any place and you can define them anywhere, but it is not possible to define a variable more than once.
- Declaring variables is very simple, however, defining variables is difficult.
Declaration
A declaration defines a variable and it is also known as a statement. When you declare a variable it means that you are declaring a name for the variable. The names of the variables are used to identify it.
In C programming language, a declaration is written as a keyword followed by the variable name and then it is followed by the type.
For example,
int x = 10;
Here the variable named x has been declared with the type int. Declaring a variable means that it is defined. It also means that the value for the variable is already determined.
Definition
After the declaration the next step is to define the variable. To define a variable, you need to assign a value to it. The value is assigned in the following order:
The first operand is assigned to the variable and then the second operand is assigned.
Example:
x = 5;
y = x + 5;
Here the variable x is initialized to the value 5 and then it is assigned to the value 5 + 5. If you don’t initialize the variable then it will have a default value.
The value of a variable is also known as the initial value of a variable. In most of the programming languages the value of a variable is 0 by default. But in some programming languages it is given some other values like -1, 1, 2 etc.
Declaration and definition are both very important in programming. If you don’t know the difference between them then you will have to spend a lot of time.
The difference between declaration and definition of a variable is that declaration is done at the top of the code whereas the definition is done at the point of use.
So, what is the difference between declaration and definition of a variable? Let’s find out.
What is the difference between declaration and definition of a variable?
The main difference between declaration and definition is that declaration is done at the top of the program whereas definition is done at the point of use.
What does it mean?
If you have declared the variable before you can use it, then you can use it. If you have defined the variable at the point of use then you cannot use it.
Examples
Example 1:
class A {
int x = 2;
}
Example 2:
int x = 1;
Example 3:
void func(int x)
{
x = 2;
}
Why declaration is done at the top of the program?
Declaration is the first statement that is written in a programming language. So, you need to declare the variables before you can use them.
I hope you liked this post and now you know the difference between declaration and definition of a variable..