Declaration and Implementation of Functions

c++, implementation, user-defined-functions

Solution

If you don't declare the forward declarations ("prototypes"), then you need to make sure that all your functions occur before any functions that depend on them, i.e. in the reverse order of the call graph. That's fine for a simple example as above, but is a complete pain for anything more realistic (and in some cases impossible, if there are any loops in the call graph).

Problem

According to my teacher, it's bad practice to write user-defined functions like this: ``` int DoubleNumber(int Number) { return Number * 2; } int main() { cout << DoubleNumber(8); } ``` Instead, he says to always use forward declarations, even if the functions don't need any knowledge of each other: ``` int DoubleNumber(int Number); // Forward declaration. int main() { cout << DoubleNumber(8); } int DoubleNumber(int Number) // Implementation. { return Number * 2; } ``` I find this especially strange since he made a point of telling us how important it is that the forward declaration and implementation are exactly the same or you'll get errors. If it's such a big deal, why not just put it all above `main()`? So, is it really bad practice to declare and implement at the same time? Does it even matter?

Original source