Noob question about Defining and Declaring.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 9
Reputation: Maple_Tiger is an unknown quantity at this point 
Solved Threads: 0
Maple_Tiger Maple_Tiger is offline Offline
Newbie Poster

Noob question about Defining and Declaring.

 
0
  #1
Mar 27th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Noob question about Defining and Declaring.

 
0
  #2
Mar 27th, 2005
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).
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: dave2point0 is an unknown quantity at this point 
Solved Threads: 0
dave2point0 dave2point0 is offline Offline
Newbie Poster

Re: Noob question about Defining and Declaring.

 
0
  #3
Mar 27th, 2005
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)

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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Noob question about Defining and Declaring.

 
0
  #4
Mar 27th, 2005
>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:
  1. void f(); // Declaration
A function definition does include the function body:
  1. void f()
  2. {
  3. // Shtuff
  4. }
Now, because a definition is always a declaration, you can do this:
  1. // No explicit declaration
  2.  
  3. void f()
  4. {
  5. // Shtuff
  6. }
  7.  
  8. int main()
  9. {
  10. f();
  11. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: dave2point0 is an unknown quantity at this point 
Solved Threads: 0
dave2point0 dave2point0 is offline Offline
Newbie Poster

Re: Noob question about Defining and Declaring.

 
0
  #5
Mar 27th, 2005
Yeh, sorry about that - I know what I mean but I am extremely bad with keywords and technical jargon

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:
  1. void f(); // Declaration
A function definition does include the function body:
  1. void f()
  2. {
  3. // Shtuff
  4. }
Now, because a definition is always a declaration, you can do this:
  1. // No explicit declaration
  2.  
  3. void f()
  4. {
  5. // Shtuff
  6. }
  7.  
  8. int main()
  9. {
  10. f();
  11. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 9
Reputation: Maple_Tiger is an unknown quantity at this point 
Solved Threads: 0
Maple_Tiger Maple_Tiger is offline Offline
Newbie Poster

Re: Noob question about Defining and Declaring.

 
0
  #6
Mar 27th, 2005
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC