944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4440
  • C++ RSS
Mar 27th, 2005
0

Noob question about Defining and Declaring.

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maple_Tiger is offline Offline
9 posts
since Mar 2005
Mar 27th, 2005
0

Re: Noob question about Defining and Declaring.

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).
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 27th, 2005
0

Re: Noob question about Defining and Declaring.

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)

Quote originally posted by Narue ...
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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dave2point0 is offline Offline
6 posts
since Mar 2005
Mar 27th, 2005
0

Re: Noob question about Defining and Declaring.

>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:
C++ Syntax (Toggle Plain Text)
  1. void f(); // Declaration
A function definition does include the function body:
C++ Syntax (Toggle Plain Text)
  1. void f()
  2. {
  3. // Shtuff
  4. }
Now, because a definition is always a declaration, you can do this:
C++ Syntax (Toggle Plain Text)
  1. // No explicit declaration
  2.  
  3. void f()
  4. {
  5. // Shtuff
  6. }
  7.  
  8. int main()
  9. {
  10. f();
  11. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 27th, 2005
0

Re: Noob question about Defining and Declaring.

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

Quote originally posted by Narue ...
>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:
C++ Syntax (Toggle Plain Text)
  1. void f(); // Declaration
A function definition does include the function body:
C++ Syntax (Toggle Plain Text)
  1. void f()
  2. {
  3. // Shtuff
  4. }
Now, because a definition is always a declaration, you can do this:
C++ Syntax (Toggle Plain Text)
  1. // No explicit declaration
  2.  
  3. void f()
  4. {
  5. // Shtuff
  6. }
  7.  
  8. int main()
  9. {
  10. f();
  11. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dave2point0 is offline Offline
6 posts
since Mar 2005
Mar 27th, 2005
0

Re: Noob question about Defining and Declaring.

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maple_Tiger is offline Offline
9 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Mortgage Calculations
Next Thread in C++ Forum Timeline: Help With A Timer And Stopping It.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC