| | |
Noob question about Defining and Declaring.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 9
Reputation:
Solved Threads: 0
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:
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).
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.
•
•
Join Date: Mar 2005
Posts: 6
Reputation:
Solved Threads: 0
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)
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).
>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:
A function definition does include the function body:
Now, because a definition is always a declaration, you can do this:
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)
void f(); // Declaration
C++ Syntax (Toggle Plain Text)
void f() { // Shtuff }
C++ Syntax (Toggle Plain Text)
// No explicit declaration void f() { // Shtuff } int main() { f(); }
I'm here to prove you wrong.
•
•
Join Date: Mar 2005
Posts: 6
Reputation:
Solved Threads: 0
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:
A function definition does include the function body:C++ Syntax (Toggle Plain Text)
void f(); // Declaration
Now, because a definition is always a declaration, you can do this:C++ Syntax (Toggle Plain Text)
void f() { // Shtuff }
C++ Syntax (Toggle Plain Text)
// No explicit declaration void f() { // Shtuff } int main() { f(); }
•
•
Join Date: Mar 2005
Posts: 9
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Mortgage Calculations
- Next Thread: Help With A Timer And Stopping It.
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






