Thread: difference between definition, declaration & prototype

  1. #1
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Montreal, Canada

    difference between definition, declaration & prototype

    Searching for content on this forum before creating a thread I stumbled on const function which post #2-3 caught my eye.
    Quote Originally Posted by grumpy
    A "function definition" has a specific meaning in C++, which I suspect you have misunderstood.
    Quote Originally Posted by neutron_star
    sorry its function prototype.
    I then made a few searches on the separate terms but for most interesting sites, the definitions seemed to contradict one another. I then finally tried my luck searching what I really what to know: c+++function+definition+declaration+prototype+diff erence OR compare OR comparison OR versus OR VS ; that is the difference between definition, declaration & prototype. I have my own assumptions but I won't share it because they'd very likely hamper the issue instead of helping it.

  2. #2
    Registered User
    Join Date
    Oct 2006
    prototype applies specifically to functions, and in that context, a prototype is the same as a declaration:
    int getSomeNumber();
    a class can have a (forward) declaration:
    class someClass;
    but the class definition would be like so:
    class someClass
    now a function definition is the part that actually contains the code, like from our above example:
    int getSomeNumber()
        return 3; // 3 is the most random number available
    a function definition is also sometimes referred to as its implementation, and defining/implementing a standalone function - that is, not a class member - effectively also declares it.

    note that in my class example that the constructor is declared, but there is no code, so it is not defined, although the class can be considered defined at this point. the definition/implementation of the constructor might look like this:
        // do some initialization

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    A function declaration is a prototype.

    A function definition is a function with a heading and a body.

    That's all.

    Sometimes, a function declaration can be a definition, depending on whether you wrote a prototype, and if you wrote a function in the right place. One of the first things you learn about functions is that they need to be declared before they are called. Well, if you define a function before it is called, that works too.

    In common conversation it is better to ask for clarification if you are confused. Sometimes the distinction between the terms is ignored because it isn't exactly ambiguous what somebody means.

    [edit]More generally, a definition is considered to be the line of code that allocates resources for the thing. A declaration will just reserve a name.[/edit]

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Rather than try and describe the grammar to you formally, here's an example of a function definition:
    void foo() {}
    A function definition is also a declaration of the function, but while there can only be one definition of a given function, there can be many declarations of the same function.

    Here's an example of a function declaration that is not a function definition:
    void foo();
    A function declaration that is not a function definition is also known as a function prototype when it is used as a forward declaration (i.e., it is declared before some code that calls the function or takes a pointer to the function).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between definition and declaration
    By Allen in forum C++ Programming
    Replies: 4
    Last Post: 02-02-2012, 03:53 AM
  2. Difference between Declaration , initialisation and definition.
    By nkrao123@gmail. in forum C Programming
    Replies: 12
    Last Post: 09-11-2011, 10:07 AM
  3. Prototype Declaration
    By rrahulvverma in forum C Programming
    Replies: 6
    Last Post: 10-13-2010, 12:18 PM
  4. Why put the prototype inside the function definition?
    By Overworked_PhD in forum C Programming
    Replies: 14
    Last Post: 01-11-2010, 01:48 PM
  5. Function Prototype and Definition
    By XDarklytez in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2005, 01:28 PM

Tags for this Thread