Ad Code

Definition, Declaration, Intialization

What is definition , declaration and initialization ?


A declaration is a statement that says "here is the name of something and the type of thing that it is, but I'm not telling you anything more about it".

A definition is a statement that says "here is the name of something and what exactly it is". For functions, this would be the function body; for global variables, this would be the translation unit in which the variable resides.

An initialization is a definition where the variable is also given an initial value. Some languages automatically initialize all variables to some default value such as 0, false, or null. Some (like C/C++) don't in all cases: all global variables are default-initialized, but local variables on the stack and dynamically allocated variables on the heap are NOT default initialized - they have undefined contents, so you must explicitly initialize them. C++ also has default constructors, which is a whole nother can of worms.

Examples:


// In global scope:
extern int a_global_variable;  // declaration of a global variable
int a_global_variable;         // definition of a global variable
int a_global_variable = 3;     // definition & initialization of a global variable

int some_function(int param);  // declaration of a function
int some_function(int param)    // definition of a function
{
    return param + 1;
}
Reactions

Post a Comment

0 Comments