Declaration

Updated: 03/06/2020 by Computer Hope

A declaration or declare may refer to any of the following:

1. In programming, a declaration is a statement describing 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.

Examples

Below are 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.

Note

Perl does not require that variables be declared unless strict is 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 indicates 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 returns an error. Similarly,

char str[30];

...declares an array named str that holds a maximum of 30 characters.

2. With a shell, declare is a builtin command of the Bash shell. See our declare command page for further information.

Declarative programming, Programming terms