Decleration and defination Problem

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

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Decleration and defination Problem

 
0
  #1
Sep 14th, 2005
In the function declarations, the argument identifiers are optional.
In the definitions, they are required (the identifiers are required only in C, not C++).

  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??
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Decleration and defination Problem

 
0
  #2
Sep 14th, 2005
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:
  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.
"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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: Decleration and defination Problem

 
0
  #3
Sep 14th, 2005
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.
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
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 3146 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC