944,142 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3417
  • C++ RSS
Sep 14th, 2005
0

Decleration and defination Problem

Expand Post »
In the function declarations, the argument identifiers are optional.
In the definitions, they are required (the identifiers are required only in C, not C++).

C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2.  
  3. void f(int);
  4.  
  5. int main()
  6. {
  7. f(10);
  8. return 0;
  9. }
  10.  
  11. void f(int)
  12. {
  13. }
<< moderator edit: added [code][/code] tags >>

This works fine..where is 10 going??..how the compiler handles this??
Similar Threads
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 14th, 2005
0

Re: Decleration and defination Problem

Quote originally posted by sunnypalsingh ...
This works fine..where is 10 going??..how the compiler handles this??
The same as it would any other useless code, such as:
C++ Syntax (Toggle Plain Text)
  1. int y, x = 10;
  2. x = 5;
In the case of calling a function with a parameter, the value is still passed -- just like it would be if the value were being used.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 14th, 2005
0

Re: Decleration and defination Problem

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.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 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: Pointer, vector, and/or deque problem
Next Thread in C++ Forum Timeline: Project Ideas?





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


Follow us on Twitter


© 2011 DaniWeb® LLC