views:

975

answers:

7

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?

+5  A: 

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
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
That is a good point - from one pedant to another I appreciate the distinction :)
Andrew Hare
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
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
+3  A: 

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
+4  A: 

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
+18  A: 

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
+1 that's basically what I was going to write...
David Zaslavsky
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
Thanks for the tip, I updated the answer accordingly.
jheriko
+1  A: 

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
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
@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
+1  A: 

declare is like those guys with the great long horns that toot flourishes before the king stands up.

define is where the sausage is made.

And assignment is, forget all that, do this.

kennebec
A: 

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