I keep seeing these two words in Sams teach your self C++. Do they represent the same thing when talking about variables?

I beleave that I know what "define" means. State it's type, it's name, and semicolon. However, I see that "declare" is used for, what seems, the same meaning.

Am I missing something lol? :eek:

The difference is subtle. When you declare something, you're simply stating its name and type. When you define something, you're actually creating an object in memory. A declaration doesn't result in memory being allocated, but a definition does.

The reason they often look the same is that a definition is always a declaration (but a declaration isn't always a definition).

Yep, I read that book too ;-)

In case you didn't understand all that (and I didn't),

When talking about functions, you usually define the function at the top of a program or in a header file - this says what type the function returns, it's name, and it's arguments.

When you declare a function, you actually put all the code in that runs inside the function.

Please correct me if I'm wrong (which happens a lot)

The difference is subtle. When you declare something, you're simply stating its name and type. When you define something, you're actually creating an object in memory. A declaration doesn't result in memory being allocated, but a definition does.

The reason they often look the same is that a definition is always a declaration (but a declaration isn't always a definition).

>Please correct me if I'm wrong (which happens a lot)
Well, aside from only taking functions into consideration (declarations and definitions are used for variables too), if you swap your use of declare and define, you would be correct.

A function declaration, also called a prototype, doesn't include the function body:

void f(); // Declaration

A function definition does include the function body:

void f()
{
  // Shtuff
}

Now, because a definition is always a declaration, you can do this:

// No explicit declaration

void f()
{
  // Shtuff
}

int main()
{
  f();
}

Yeh, sorry about that - I know what I mean but I am extremely bad with keywords and technical jargon

>Please correct me if I'm wrong (which happens a lot)
Well, aside from only taking functions into consideration (declarations and definitions are used for variables too), if you swap your use of declare and define, you would be correct.

A function declaration, also called a prototype, doesn't include the function body:

void f(); // Declaration

A function definition does include the function body:

void f()
{
  // Shtuff
}

Now, because a definition is always a declaration, you can do this:

// No explicit declaration

void f()
{
  // Shtuff
}

int main()
{
  f();
}

I think I should put in a few exampls from the book lol. Me still confused.

Page 51, Analysis, line 7, smallNumber is declared to be an unsigned short int.

7: unsigned short int smallNumber;

Page 49, Analysis, line 8, Width is defined as an unsigned short integer, and it's value initialized to 5.

8: unsigend short int Width = 5;


You know, after writing this all out, I finaly unerstand what you are saying Narue. I'm such a noob.

Thanks Narue and Dave!

This article has been dead for over six months. Start a new discussion instead.