| | |
Decleration and defination Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
In the function declarations, the argument identifiers are optional.
In the definitions, they are required (the identifiers are required only in C, not C++).
<< moderator edit: added [code][/code] tags >>
This works fine..where is 10 going??..how the compiler handles this??
In the definitions, they are required (the identifiers are required only in C, not C++).
C++ Syntax (Toggle Plain Text)
#include<iostream.h> void f(int); int main() { f(10); return 0; } void f(int) { }
This works fine..where is 10 going??..how the compiler handles this??
•
•
•
•
Originally Posted by sunnypalsingh
This works fine..where is 10 going??..how the compiler handles this??
C++ Syntax (Toggle Plain Text)
int y, x = 10; x = 5;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
The compiler needs to know what something is before it will let you reference it. Just as you have to declare a variable before you use it, you must declare a function before you use it.
Imagine the code as a conversation. What if you are receiving directions on how to get somewhere, and the person giving directions told you to take a left turn at a place that you've never heard of, let's call it Johnson's Emporium. You'd be a bit confused, and you'd end up getting lost since you don't know what to look for. Now imagine that the person told you ahead of time that Johnson's Emporium was 5 miles down the road from where you are now. You'd be able to find where you need to go at that point since you know what to look for.
Some compilers allow you to use undefined variables and functions. When they see something used that they don't know about, they go looking for a definition for it. C compilers don't work that way, they need to know what to expect. By using a function declaration, you are essentially telling the compiler that there will be a function by that name and that you will define it later on.
Why would you need to use a declaration you may ask. Why not just put all the function definitions about your main function? The answer is simple. Imagine a scenario where you have two functions, and each function makes a call to the other function. It would be impossible to compile such a program is you couldn't use declarations.
I know I got a bit wordy for such a simple question, but it's important to know why code is structured as it is.
Imagine the code as a conversation. What if you are receiving directions on how to get somewhere, and the person giving directions told you to take a left turn at a place that you've never heard of, let's call it Johnson's Emporium. You'd be a bit confused, and you'd end up getting lost since you don't know what to look for. Now imagine that the person told you ahead of time that Johnson's Emporium was 5 miles down the road from where you are now. You'd be able to find where you need to go at that point since you know what to look for.
Some compilers allow you to use undefined variables and functions. When they see something used that they don't know about, they go looking for a definition for it. C compilers don't work that way, they need to know what to expect. By using a function declaration, you are essentially telling the compiler that there will be a function by that name and that you will define it later on.
Why would you need to use a declaration you may ask. Why not just put all the function definitions about your main function? The answer is simple. Imagine a scenario where you have two functions, and each function makes a call to the other function. It would be impossible to compile such a program is you couldn't use declarations.
I know I got a bit wordy for such a simple question, but it's important to know why code is structured as it is.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Abstract class homework problem (C++)
- Network security problem (Network Security)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Connection Problems (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Pointer, vector, and/or deque problem
- Next Thread: Project Ideas?
Views: 3146 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






