What is the definition for declaration respective a definition

T

Tony Johansson

Hi!

For example this snippet is a definition of a class
class Test
{
....
}

If I write
int? tal = 5;
Is this a definition or a declaration.
So when do a use the terminologi definition and when do I use the
terminologi declaration.

Does it exist any clear definition when to which one ?
//Tony
 
P

Peter Duniho

Tony said:
Hi!

For example this snippet is a definition of a class
class Test
{
....
}

If I write
int? tal = 5;
Is this a definition or a declaration.

I'd call it a declaration. I don't think I'd ever say a variable is
"defined".
So when do a use the terminologi definition and when do I use the
terminologi declaration.

Does it exist any clear definition when to which one ?

No. Even my own personal idea that a variable is never "defined", only
"declared", is mostly just my own prejudice. The words "defined" and
"declared" are ambiguous enough that you may well run into people using
them differently.

That said, I tend to use the word "declared" for simple program elements
that simply create a given identifier using existing elements, while I
use "defined" for more complex declarations that create new, reusable
pieces of a program.

Maybe that's helpful to you, or maybe it's not. :) Frankly, if someone
wanted to say that they've "defined a variable" and "declared a class",
I wouldn't spend any time trying to argue the point with them. I still
know what they mean, and that's what's important. In this particular
case, I don't think the ambiguity is likely to cause a problem.

Pete
 
T

Tom Dacon

Tony Johansson said:
Hi!

For example this snippet is a definition of a class
class Test
{
...
}

If I write
int? tal = 5;
Is this a definition or a declaration.
So when do a use the terminologi definition and when do I use the
terminologi declaration.

Does it exist any clear definition when to which one ?
//Tony

Tony, you can think of a declaration as reserving space for a variable,
without giving it a value. A definition assigns a value to a variable,
optionally declaring it at the same time according to the syntax of the
language. E.g.:

int i; declares the variable i, without assigning a specific value
i = 3; defines the value of a pre-declared variable
int i = 3; both declares and defines it

It may also be the case, depending on the language, that the compiler will
implicitly give the variable a default value, but that does not invalidate
the distinction. And the behavior varies by language, with some assigning
well-known and useful values, and others assigning random values.

This is accepted terminology, although since most people don't study
computer language theory or write compilers the distinction is rarely
observed these days, even among experienced programmers.

HTH,
Tom Dacon
Dacon Software Consulting
 
P

Peter Duniho

Tom said:
Tony, you can think of a declaration as reserving space for a variable,
without giving it a value. A definition assigns a value to a variable,
optionally declaring it at the same time according to the syntax of the
language. E.g.:

int i; declares the variable i, without assigning a specific value
i = 3; defines the value of a pre-declared variable

Except that the C# specification refers to that as "assigns", not
"defines", or "assignment" rather than "definition".
[...]
This is accepted terminology, although since most people don't study
computer language theory or write compilers the distinction is rarely
observed these days, even among experienced programmers.

Accepted by whom? According to what reference? And if "definition" is
the assignment of a value to a variable, what is the "definition" of a
class?

In C, I can declare a function:

void function(void);

Or I can define the function:

void function(void)
{
printf("foo");
}

Absent any other declaration, one might consider the definition of a
function to also be its declaration.

In fact, not that this is an authoritative source or anything, but the
first several programming-related hits on Google for "declaration
definition" directly contradict your statement:

http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.4.html
http://wiki.answers.com/Q/What_is_the_difference_between_declaration_and_a_definition_in_C
http://wiki.answers.com/Q/Difference_between_the_definition_and_declaration_of_a_variable_in_c

All of the above stipulate that a "definition" of something allocates
space for it. None suggest that actually assigning a value to a
variable has anything to do with declaration or definition.

Given my usage of "definition" for types, methods, etc. I'm much more
comfortable agreeing that one can apply the word to a declaration of a
variable where storage for the variable is allocated as part of the
declaration, than I am in saying that a variable is undefined until it's
been assigned. The variable itself is defined as soon as it's declared;
its _value_ may be undefined until assignment, but that's something else
entirely.

But all that said, just goes to show: my previous point that there may
not be great consensus on the use of the terms is reasonably valid.

Note that the C# 3.0 specification uses the word "definition" in
relation to a variable only once, and the usage in that case is
ambiguous. For variables generally, the spec uses the term "declared"
to refer to where the actual variable exists in the code, and then uses
more specific terminology to address questions such as assignment,
scope, and lifetime.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top