0
In C/C++, what is the advantage of declaring a function before defining it's body?
3 Respuestas
0
1. You can write implementation later in most single-file cases.
2. In single-file cases, it keeps things neat by allowing you to write implementation below main().
3. In multi-file cases, they're needed by header files, especially once implementation files are compiled.
4. In multi-file cases, see: https://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c
Although it's a stretch scenario, the take-home is compile errors and time wasted tracking them down.
5. Someone else may read our code. We may read it years later. Comments aside, it's very useful as a quick reference to show parameters & return (where applicable).
Just my thoughts.
+ 5
Hi,
Silas Junior,
In C & C++,
Declaring a function before function definition tells the compiler about our function. Like return type, argument list and name of the function.
; at the end of declaration is compulsory other wise compiler will show an error.
In newer version of C & C++ declaration of function is not mandatory. But we need to define the function before function call.
Thanks
+ 2
I like ordering the functions alphabetical and, therefore, defined them all first in alphabetical order so I knew what existed & could call anything, followed by their implementation.