1.

Differentiate between variable/ function declaration and definition in the context of any OOPs programming language.

Answer»

The purpose of a variable declaration is to tell the compiler of the following INFORMATION: the variable's name, the KIND of value it stores, and the initial value if any. Declaration, in other words, provides information about a variable's attributes. The definition of a variable allocates memory space for the variable and SPECIFIES where the variable will be stored.

The following table illustrates the differences between definition and declaration:

DefinitionDeclaration
Only one TIME can a variable or function be defined.There is no limit to how many times a variable or function can be declared.
Memory is allocated during definition.Memory is not allocated during declaration.

Example:

void fun(){ cout << “Hello World” << “\N”;}

The above code defines a void function named “fun” and the compiler allocates the memory for it as soon as it encounters the above code.

Example:

void fun();

The above code declares a void function named “fun”.



Discussion

No Comment Found