Thread: Difference between definition and declaration

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    19

    Difference between definition and declaration

    Who can tell me the difference between definition and declaration?
    My book said that definition is also declaration, then what's the need for C++ to distinguish definition and declaration?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Allen
    My book said that definition is also declaration, then what's the need for C++ to distinguish definition and declaration?
    Because not all declarations are definitions

    The difference is important when considering the one definition rule, e.g., you cannot define a class more than once in the same translation unit, and you cannot define the same variable or function more than once in the entire program. However, you can declare a class, variable (using extern) and function many times.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Quote Originally Posted by Allen View Post
    Who can tell me the difference between definition and declaration?
    My book said that definition is also declaration, then what's the need for C++ to distinguish definition and declaration?
    There are times when you want to just declare something without defining it. When you define something, like a variable or function, it takes up a home, i.e., a memory location with an address. So obviously, you don't want to define something twice, or else it will have two memory locations with two addresses. That would be like having two houses on the same street with the same address, and that would create ambiguity for the pizza delivery guy!

    In many cases, an object or function is already defined elsewhere, and out of sight of the compiler, so then only a declaration needs to be made so the compiler knows what it is without defining it again and creating ambiguity. An example of this would be the library functions that you use, like the iostream class for cout, etc. These functions are already defined and have a "home" and will be linked in to your program later. But for now, the compiler needs to know about it, so it just needs to be declared, not defined. The #include<iostream> header contains the declaration for these functions so the compiler can have record of them and can check to make sure the functions are being used correctly.

    Another example of when you need to declare but not define is with the functions that you write yourself. If you had two functions, say function A and function B, and function A calls function B and likewise function B calls function A, then how could you ever define these two functions? If you try to define function A first, then the compiler will complain because function A calls function B which the compiler knows nothing about. And if you try to define function B first, which calls A, then the compiler won't know what function A is and will complain. So you are forced to declare one of the functions first, say B, then you can define function A. Once function A is defined (and thus declared) you can define function B which calls A.

    Things can get hairy when you have like 10 functions in your program, and function 7 calls function 2 and 4 calls 6 and 9 calls both 1 and 2. Then when you define your functions, you have to make sure you define them all in the right order, if that's even possible, so that any one function that is defined is defined after any functions that it calls within itself. My practice is to always declare my functions first, before main() using prototypes, and then define my functions in any order later, like after main().

    With a variable, like int x, the definition is also the declaration, and you can not separate the two. With functions, you can make a declaration separate from the definition, but not so with simple variable types. Yet, there are times when you want to declare a variable without defining it again. Let's say you had two source files for your program, file A and file B, which would be combined by the linker after the compilation process (let's say your friend is writing file A and you are doing file B - it's a large program). File A is already written and in it int x is defined (and declared). You are now writing file B and you want to manipulate variable x in file A. Well, since x is already defined in file A and has a memory address already assigned to it, you only need to declare it in file B so the compiler knows about it and accepts it. And since you can't separate the definition from the declaration, you need to add a keyword to the variable name. This keyword is extern, and is used like this: extern int x;. This tells the compiler to look at int x only as a declaration and not as a definition, because it is already defined elsewhere (externally). This way, if you wanted to, you could declare a variable before main(), use it in main(), then actually define it after main(), just like a function.

    So in summary, there are many times when something has already been brought into existence (defined) but needs to be declared, or has yet to be brought into existence but is being used anyway, thus it needs to be declared so the compiler knows about it and knows how to code it (i.e., translate it).

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Nice explanation!
    An example of this would be the library functions that you use, like the iostream class for cout, etc. These functions are already defined and have a "home" and will be linked in to your program later. But for now, the compiler needs to know about it, so it just needs to be declared, not defined. The <iostream> header contains the declaration for these functions so the compiler can have record of them and can check to make sure the functions are being used correctly.
    Well, they(i.e cin, cout..etc) aren't functions but objects.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between Declaration , initialisation and definition.
    By nkrao123@gmail. in forum C Programming
    Replies: 12
    Last Post: 09-11-2011, 10:07 AM
  2. declaration or definition
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 07:50 PM
  3. definition/declaration/...
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2007, 02:38 PM
  4. Declaration vrs Definition
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 07:13 PM
  5. Declaration vs definition
    By ripper079 in forum C++ Programming
    Replies: 8
    Last Post: 03-15-2002, 11:00 PM