Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration and Implementation of Functions

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?

like image 787
Maxpm Avatar asked Dec 08 '10 19:12

Maxpm


4 Answers

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).

like image 151
Oliver Charlesworth Avatar answered Sep 22 '22 22:09

Oliver Charlesworth


I think your teacher is an old C programmer.

If you wrote a C program without forward declarations and one function called another function declared later in the file (or in a different compilation unit), the compiler would not complain but silently pretend to know what the prototype should be.

Debugging is horrible, if you don't know if your compiler is passing the arguments correctly. Therefore it was a good defensive policy to always declare all functions; at least the compiler could raise an error if the declaration did not match the implementation.

C compilers and tool have gotten better (I hope). It is still not an error to call an unknown function, but GCC for example is kind enough to warn by default.

But in C++ you can't call a function that hasn't been declared or defined. Consequently, C++ programmers don't worry much about forward declarations.

like image 41
antonakos Avatar answered Sep 22 '22 22:09

antonakos


Your teacher's policy is horrible IMHO. Use forward declarations only when they're really needed. That way, their presence demonstrates their necessity, which gives the reader useful documentation (i.e., there may be mutual recursion between the functions). Of course you do need forward declarations in header files; that's what they're for.

like image 20
Karl Knechtel Avatar answered Sep 25 '22 22:09

Karl Knechtel


In my first programming class, the teacher also emphasized this point. I'm not exactly sure there is a benefit to such a simple case in actual software.

However, it does prepare you for using header files if you haven't covered that yet. In a typical case, you will have a header file custom-math.h and source file custom-math.cpp where custom-math.h contains the forward declaration and custom-math.cpp the implementation. Doing so may increase compilation time significantly when doing modifications to function implementations only in large projects. It is also a convenient way to split your program into "logical" groups of functions and/or classes.

If you are going to put other functions in the same file as main(), then what you do probably depends on your personal preference. Some people prefer to have main() close to the top to get to the program logic right away. In this case, forward declare your functions.

like image 27
André Caron Avatar answered Sep 26 '22 22:09

André Caron