What is the difference between definition, declaration and initialization?
I will answer this question in a general and complete way and not with respect to any programming language
There is a hell of a lot of confusion between declaration, definition, and initialization. Sometimes they all look similar and sometimes completely different.
Before understanding the differences, It is very important to be aware of two things:
- The difference between declaration, definition, and initialization varies from one programming language to another. Each programming has its own way of doing these three things.
- The “thing” which you are defining, declaring, or initializing also affects the difference between the three of them. That “thing” can be a variable, a class, or a function. All of them have different meanings of definitions, declaration, and initialization.
Once we are aware of the above two things, most of the doubts get cleared and we stop seeking exact differences because it’s not there.
In general terms ( irrespective of any language or “thing”)
- The declaration means we are saying to a computer that this “thing” (it can be a variable, a function or a class) exists but we don’t know where. In the future, we may tell but right now it just exists somewhere. In simple words, we don’t allocate memory while declaring. We can declare that “thing” many times.
- The definition means we are saying to the computer that this “thing” needs memory and it needs to be located somewhere. In simple words, defining means we have allocated memory for it. We can define something only once
- The initialization means whatever our “thing “ is, we are giving it an initial value. That “thing” must be in some memory location and if we keep that location empty, it may be a house for bugs and errors. Initialization is not always necessary but it’s important.
Many people assume that declaration + definition = Initialization .
It's not wrong, but it’s not correct in all places. It's correct only for variables that too in a language like C ++ or maybe C.
In python, there is no concept of the declaration. We don’t need to declare anything in it.
The general meaning of the three is valid everywhere but the way that is performed varies from language to language and the “thing”.
Hope it helps :)
Comments
Post a Comment