Is function prototyping dead?
-
A semi-popular IDE compiles my C++ code without declaring function prototype. Did I missed the newsflash or just an compiler option? Seems odd.
-
A semi-popular IDE compiles my C++ code without declaring function prototype. Did I missed the newsflash or just an compiler option? Seems odd.
A function definition is also a declaration if there was none so far. The compiler (not the IDE) will add it to it's internal declaration list when processing such a function.
-
You only need prototypes for forward declarations. And IDEs do not do the compiling, that's the compiler's job.
True, but I have been using IDE whose authors are very fond of their "beginners feature " which actually builds prototypes "for you". So they claim. I am now test driving another IDE, still based on GCC and the first test function I used compiled without prototype, that is why I made this post. Next function failed without prototype, so I am not sure what is going on. So what is the difference between prototype and forward declaration? Or is there a difference?
-
True, but I have been using IDE whose authors are very fond of their "beginners feature " which actually builds prototypes "for you". So they claim. I am now test driving another IDE, still based on GCC and the first test function I used compiled without prototype, that is why I made this post. Next function failed without prototype, so I am not sure what is going on. So what is the difference between prototype and forward declaration? Or is there a difference?
When the compiler comes across a function call it checks its tables for a declared prototype. If it cannot find one then it will infer a definition from the parameters of the function call. When it then finds the actual function it will check that the definition matches the stored declaration. If it does, then all well and good. If it does not then it will throw out an error message. As to your last question, a prototype is a forward declaration. It just tells the compiler the type of the parameters and return value of the function, so it can check on any calls made to the function before it is found in the source. The same applies to external functions whose prototypes are in .h files.
-
True, but I have been using IDE whose authors are very fond of their "beginners feature " which actually builds prototypes "for you". So they claim. I am now test driving another IDE, still based on GCC and the first test function I used compiled without prototype, that is why I made this post. Next function failed without prototype, so I am not sure what is going on. So what is the difference between prototype and forward declaration? Or is there a difference?
A declaration (prototype) describes an object (function, class, variable).
int some_function(int some_arg);
A definition (implementation) implements an object.
int some_function(int some_arg)
{
int result = 0;
// some code
return result;
}If there is no declaration for an object, the definition implies the declaration. The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. While strictly speaking all declarations are forward declarations, the term is mainly used with function declarations in header files where a pointer or reference to another class is passed as function argument. An example:
// Header file for MyClass
// Forward declaration for OtherClass.
class OtherClass;class MyClass
{
MyClass();
SomeFunction(OtherClass &otherClass);
};The implementation file of
MyClass
must still include the header file containing theOtherClass
declaration. But the above header file must not do so which reduces the compile time. More important, this must be used when both classes use the other which would normally require that both header files include each other which would lock the compiler (recursive inclusion). See also this SO thread: c++ - What is the difference between a definition and a declaration? - Stack Overflow[^] -
A declaration (prototype) describes an object (function, class, variable).
int some_function(int some_arg);
A definition (implementation) implements an object.
int some_function(int some_arg)
{
int result = 0;
// some code
return result;
}If there is no declaration for an object, the definition implies the declaration. The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. While strictly speaking all declarations are forward declarations, the term is mainly used with function declarations in header files where a pointer or reference to another class is passed as function argument. An example:
// Header file for MyClass
// Forward declaration for OtherClass.
class OtherClass;class MyClass
{
MyClass();
SomeFunction(OtherClass &otherClass);
};The implementation file of
MyClass
must still include the header file containing theOtherClass
declaration. But the above header file must not do so which reduces the compile time. More important, this must be used when both classes use the other which would normally require that both header files include each other which would lock the compiler (recursive inclusion). See also this SO thread: c++ - What is the difference between a definition and a declaration? - Stack Overflow[^]The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. Perfect answer - that is exactly why my test function - first one in header file - did not need declaration. Jochen, I really appreciate your help - always right to the point. Thanks PS Are you by any chance teacher / instructor ? You sure know how to explain stuff. Cheers Vaclav
-
The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. Perfect answer - that is exactly why my test function - first one in header file - did not need declaration. Jochen, I really appreciate your help - always right to the point. Thanks PS Are you by any chance teacher / instructor ? You sure know how to explain stuff. Cheers Vaclav
Thank you. I'm not a teacher. I'm an R&D engineer (hardware and software).