16

I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for the first time I assign to a variable? Or is there more to it than that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

8 Answers8

40

A definition is where a value or function is described, i.e. the compiler or programmer is told precisely what it is, e.g.

int foo()
{
  return 1;
}

int var; // or, e.g. int var = 5; but this is clearer.

A declaration tells the compiler, or programmer that the function or variable exists. e.g.

int foo();
extern int var;

An assignment is when a variable has its value set, usually with the = operator. e.g.

a = b;
a = foo();
jheriko
  • 3,043
  • 1
  • 21
  • 28
  • 3
    Actually I would consider "int var;" to be definition, "int var = 5;" is a combined def/ass. Basically, in C, definition is anything that creates space for an object. I'll upvote anyway, maybe you'll change the answer, maybe not, but it's the best to date. – paxdiablo Mar 23 '09 at 01:09
9

Define and declare are similar but assign is very different.

Here I am declaring (or defining) a variable:

int x;

Here I am assigning a value to that variable:

x = 0;

Here I am doing both in one statement:

int x = 0;

Note

Not all languages support declaration and assignment in one statement:

T-SQL

declare x int;
set x = 0;

Some languages require that you assign a value to a variable upon declaration. This requirement allows the compiler or interpreter of the language to infer a type for the variable:

Python

x = 0
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • I'll split a hair -- Python has no "declaration" of any kind. Classes and functions are "defined", variables are created when they assigned. You don't really "declare" variables, since they're just names assigned to objects. – S.Lott Mar 23 '09 at 00:58
  • That is a good point - from one pedant to another I appreciate the distinction :) – Andrew Hare Mar 23 '09 at 01:02
  • More nitpicking: Python does not really do any type inference, it's dynamically typed. Type inference is only required when the variable has a type. This is valid Python and illustrates the effects of my point: `x = 5 ; x = "str"` – Magnus Hoff Mar 23 '09 at 13:24
  • I suggest either using a different language as an example (Haskell or C# with var), or just remove the "inference"-sentence. Anyway, this is just nitpicking :) – Magnus Hoff Mar 23 '09 at 13:24
  • 2
    -1 for being the accepted answer yet being wrong: declare and define are _not_ at all similar (even if in some languages the distinction is not made). See jheriko's answer below. – Ian Goldby Nov 04 '11 at 15:05
  • It is probably confusing to provide answers using simple types such as integers. The term define might not apply to them and if so then it is not possible to explain a distinction using simple types. – Sam Hobbs Feb 09 '13 at 21:19
6

It is important to use the correct terminology, otherwise people will not know what you are talking about, or incorrectly assume that you don't know what you are talking about.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
5

These terms often have precise meanings in the standards for various languages. When that is the case they should not be conflated.

In c for instance:

  • a function may be defined only once (when you say what it does), but it may also be declared before that (when you say what arguments it takes and what type it returns).

  • likewise a variable is declared when you say what type it is, and this happens only once for each scope. But you may assign a value repeatedly. (Some languages also differentiate between initialization (giving a variable a value at declaration time) and assignment (changing the value later).)

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
4

General Role: Definition = declaration + reserved space.

Definition, declaration, and assignment have two cases:

  1. for Variables.
  2. for Functions.

For Variables:

-- Definition:
To tell the compiler to reserve memory for the variable.

int x;

-- Declaration:
To tell the compiler that the variable defined in somewhere else.

extern int x;

-- Assignment:
To tell the compiler to put the value in the variable.

x = 0;

For Functions:

-- Definition:

int functionDef(int x){
  int x;  
  ...  
  ...  
  ...  
  return x;  
}

-- Declaration: It is just the prototype of the function.

int functionDef(int x);
3

The differences can seem subtle, but they are important. Not every language makes the same distinctions, but in C++ a variable declaration makes the type and name of the variable known to the compiler

int i;

A variable definition allocates storage and specifies an initial value for the variable.

i = 1;

You can combine a variable declaration and definition into one statement, as is commonly done.

int x = 1;

Declaring a variable inside a function will also set aside memory for the variable, so the following code implicitly defines variable a as a part of its declaration.

int main()
{
    int a;
    return 0;
}

Since variable a is automatically defined by the compiler, it will contain whatever value was in the memory location that was allocated for it. This is why it is not safe to use automatic variables until you've explicitly assigned a known value to them.

An assignment takes place any time you change the value of a variable in your program.

x = 2;
x++;
x += 4;

A function declaration, similar to the variable declaration, makes the function signature known to the compiler. This allows you to call a function in your source code before it is defined without causing a compiler error.

int doSomething(float x);

A function definition specifies the return type, name, parameter list, and instructions for a function. The first three of these elements must match the function declaration. A function must only be defined once in a given program.

int doSomething(float x)
{
    if( x < 0 )
    {
        x = -x;
    }
    return static_cast<int>(x);
}

You can combine the function decalartion and definition into one, but you must do so before the function is called anywhere in your program.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • I thought a variable *declaration* assigned storage space. You just get garbage in that space unless you also provide an initialisation, or until you assign a new value. You can only initialise a variable whilst declaring it. – Artelius Mar 23 '09 at 01:44
  • @Artelius: In most cases declaration includes an implicit definition by the compiler, which makes it appear that declaration assigns storage space. I made a (hopefully) clarifying addition to include this in my answer. Thanks for pointing it out. – Bill the Lizard Mar 23 '09 at 13:50
  • I voted this answer up as it is correct as I've learned it. If not, I'd really like to know why. The answers above have declaration and definition backwards. – lakeweb Jan 29 '17 at 17:31
  • `i = 1` is an assignment, not a definition. To be a definition (e.g. of a previously declared `extern` variable in its defining translation unit), it would need the type (and the assignment component is optional). – underscore_d Jun 29 '17 at 16:17
-1

It might depend on the language, as has been said. I think it really depends on whether the words are used for things like classes. For most of the data types discussed here, the question might not have much relevance. In C++ (see c++ - What is the difference between a definition and a declaration?), a class or struct always has precisely one definition but can be declared zero or more times. A class cannot be declared without a definition. So "declared" might be synonymous with "used".

In most languages, simple types such as integers do not need definitions in the manner that classes do.

Community
  • 1
  • 1
Sam Hobbs
  • 83
  • 1
  • 6
-2

The correct answer depends on which language you're talking about. Computer languages often have specific terminology, either because of the language specification or the community grown up around the language. COBOL, back when I used it, had a much different terminology than more mainstream languages (in the sense of languages closer to the mainstream of language development, not mainstream business). Forth developed some strange terminology.

If you know English, you can usually get a good idea as to what a word means from its normal meaning, but never count on it too much. The same is true with specific words across languages or language communities.

David Thornley
  • 56,304
  • 9
  • 91
  • 158