• Advertisement
Sign in to follow this  

C++, prototypes vs declarations [solved]

This topic is 4408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I'm just a little confused when it comes to terminology, and more specificly prototypes and declarations.
Quote:
Essentially, you learned about function prototyping (without naming it as such) in the previous chapter, since the form of function declaration in C++ requires proper prototyping. In a function prototype, the argument list contains the types of arguments that must be passed to the function and (optionally for the declaration) identifiers for the arguments. The order and type of the arguments must match in the declaration, definition, and function call. Here’s an example of a function prototype in a declaration: int translate(float x, float y, float z);
And from another part of the book:
Quote:
float f(float); // Function declaration
This just makes me confused. What's a prototype exactly and what's a declaration? [Edited by - tufflax on December 29, 2005 7:16:23 AM]

Share this post


Link to post
Share on other sites
Advertisement
A function prototype is the same as a function declaration. The term "prototype" applies to free functions only, since it's not applicable to structs and classes.
The term "declaration", however, applies to both classes/structs and functions.

For functions, the declaration/prototype is the signature of the function, e.g. its name and its parameters (order and type only - names are optional) [edit] and return type[/edit].

The definition consists of the signature along with the body (e.g. implementation) of the function. Each used parameter must have a unique name in the signature in order to access it.

Example:

// function declarations
void dostuff(int, float);
int calcstuff(int x);

// function definitions
void dostuff(int x, float) {
// the second parameter is not used -> no name required; this is useful
// for callbacks that do not use every parameter but must have the same
// signature
...
}

int calcstuff(int z) {
// the parameter name in the definition does not have to match the declaration
// you might want to include parameter names in the declaration anyway
// since it makes the usage of the function more obvious
return 4*z;
}




HTH,
Pat.

Share this post


Link to post
Share on other sites
Quote:
Original post by tufflax
This just makes me confused. What's a prototype exactly and what's a declaration?


"prototype" is a C term. In C++ it's called a declaration. People who use the term "prototype" in C++ came from a C background. Treat what they say with some suspicion, since C != c++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
"prototype" is a C term. In C++ it's called a declaration. People who use the term "prototype" in C++ came from a C background. Treat what they say with some suspicion, since C != c++.


Then I suppose we should treat everyone on the C++ Standards Committee with suspician because they used the term prototype in the C++ Standard?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
prototype is alright in a C or a C++ world... after all C is a subset of C++

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Me again.

Then again C came first. so C++ is a super set of C.

Share this post


Link to post
Share on other sites
Neither is true. There is plenty of C that isn't legal C++ and vice versa.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Then I suppose we should treat everyone on the C++ Standards Committee with suspician because they used the term prototype in the C++ Standard?


Don't forget that a person who had never written a single line of C++ in his entire life actually managed to invent the C++ language. What does that tell us about the C++ Standards Committee? [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement