Updated: 06/30/2019 by Computer Hope

In programming, a declaration is a statement that describes an identifier, such as the name of a variable or a function. Declarations are important because they inform the compiler or interpreter what the identifying word means, and how the identified thing should be used.

A declaration may be optional or required, depending on the programming language. For example, in the C programming language, all variables must be declared with a specific data type before they can be assigned a value.

Below are some examples of declarations.


use strict;
my $help;

The above perl statement declares a variable named $help. The dollar sign ($) indicates that the variable is a scalar. The special word my declares that $help has local lexical scope, meaning that outside the enclosing code block, the variable $help cannot be used.


Perl does not require that variables be declared unless strict is being used. However, we always recommend using strict and declaring variable to help prevent problems with your code.

Similarly, this Perl declaration:

our $help;

...uses the special word our to declare that the scalar $help has global lexical scope. This declaration means that the variable can be used anywhere in the program, and any part of the program can change the value of $help.

In some languages, a declaration and a value assignment can occur in a single statement. For example, in perl:

my $help = "Computer Hope";

...declares a scalar variable $help with local scope, and assigns it the string value Computer Hope.

In the C programming language, this declaration:

int x;

...uses the special word int to declare that x is a variable of the integer data type. If the program tries to assign a non-integer value to x, the compiler will return an error. Similarly,

char str[30];

...declares an array named str which can hold a maximum of 30 characters.

Programming terms