Conceptual ambiguity "function prototype"

This is a discussion on Conceptual ambiguity "function prototype" within the C Programming forums, part of the General Programming Boards category; Hi I found some ambiguities about the concept "function prototype". MSDN tells me this: Code: A function declaration precedes the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    Conceptual ambiguity "function prototype"

    Hi
    I found some ambiguities about the concept "function prototype". MSDN tells me this:
    Code:
    A function declaration precedes the function definition and specifies the name, return type, storage class, and other attributes of a function. 
    To be a prototype, the function declaration must also establish types and identifiers for the function's arguments.
    Whereas many other sources tell this:
    Code:
    A function prototype is a declaration of a function that declares the types of its parameters.
    So to be a function prototype, do "the identifiers for the function's arguments" need to be specified or not? Or just providing parameter types is enough to be a function prototype? I think this is also a problem of the choice of parameter-declaration:
    Code:
    parameter-declaration: 
    declaration-specifiers declarator                           // named
    declaration-specifiers abstract-declarator<opt>   // anonymous
    Can the anonymous be used in a function prototype?

    At last, what's necessary information for a function prototype if not? Now I think only parameter types are needed, right?

    Thanks in advance!
    Last edited by password636; 04-02-2009 at 12:38 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You've missed one key thing, function prototype != function declaration. Those two quotes are not specifically discussing the same thing, although they're [very] similar.

    > Or just providing parameter types is enough to be a function prototype?
    Yes.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by zacs7 View Post
    You've missed one key thing, function prototype != function declaration. Those two quotes are not specifically discussing the same thing, although they're [very] similar.
    So can I think this way: a function declaration or definition which provides the parameter types is taken to be a function prototype by the compiler. The compiler then uses the prototype to check parameters for subsequent function calls.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by password636 View Post
    So can I think this way: a function declaration or definition which provides the parameter types is taken to be a function prototype by the compiler. The compiler then uses the prototype to check parameters for subsequent function calls.
    Yes. The confusion in this area usually comes from the declaration/definition distinction. All definitions are also declarations, but not all declarations are definitions. A declaration which is not a definition is a prototype.

    The purpose of prototypes is to indicate the specific calling signature for the function in question. This includes the parameter types, but in the case of member functions, also includes CV qualifiers (const and volatile) as applied to the function as a whole.

    In C, prototypes are optional, although the compiler may warn if you forget them. If a prototype is given, it MUST match the definition which comes later on.

    In C++, ALL functions must be declared at the point of use. A prototype or previous definition is sufficient.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Quote Originally Posted by brewbuck View Post
    In C, prototypes are optional, although the compiler may warn if you forget them. If a prototype is given, it MUST match the definition which comes later on.
    Only in C90, though. In C99, the implicit int and implicit function declarations are errors.
    So it is GOOD practice to always use prototypes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ambiguity error with double as parameter type
    By tejasvsrinivasa in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2007, 03:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21