Declarations provide information about the name and type of data objects needed during program execution.
Two types of declaration:
- implicit declaration
- explicit declaration
Implicit declaration or default declaration:
They are those declaration which is done by compiler when no explicit declaration or user defined declaration is mentioned.
In 'perl' compiler implicitly understand that
$abc ='astring' is a string variable and
$abc=7; is an integer variable.
Explicit declaration of data object:
It is an example of Float A,B, of c language. In explicit we or user explicitly defined the variable type. In this example it specifies that it is of float type of variable which has name A & B.
A "Declaration" basically serves to indicate the desired lifetime of data objects.
Declarations of operations:
- compiler need the signature of a prototype of a subprogram Or function so it can determine the type of argument is being used and what will be the result type.
* Before the calling of subprogram, Translator need to know all these information. *
Example in C language
Float sub(int z, float y)
It declares sub to have the signature
Sub: int xfloat-> float
Purpose of Declarations:
1) Choice of storage representation: AS Translator determine the best storage representation of data types that why it needs to know primarily the information of data type and attribute of a data object.
2) Storage Management: It make to us to use best storage management for data object by providing its information and these information as tells the lifetime of a data object.
In C language we have many options for declaration for elementary data type.
1) Simple Declaration:Like float A,B;
It tells lifetime is only at the end of execution as lifetime of every data objects can be maximum to end of execution time.
But simple declaration tells the single block of memory will be allocated.
2) Runtime Declaration: C language and many more language provide us the feature of dynamic memory allocation by keywords "Malloc and Calloc."
So in this special block of memory is allocated in memory and their lifetime is also different.
3) Polymorphic operations: In most language, some special symbol like + to designate any one of the several different operation which depends on type of data or argument is provided.
In this operation has some name like as we discussed + in this case operation symbol is said to be overloaded because it does not designate one specific operation.
Ada: allows programmer to overload subprograms.
ML: Expands this concept with full polymorphism where function has one name but variety of implementation depending on the types of arguments.
4) Type checking:- Declaration is basically for static type checking rather than dynamic.