0

In C/C++, what is the advantage of declaring a function before defining it's body?

7th Jul 2018, 8:25 PM
Silas Junior
Silas Junior - avatar
3 odpowiedzi
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.
7th Jul 2018, 11:59 PM
non
+ 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
7th Jul 2018, 8:37 PM
Deepak Kumar
Deepak Kumar - avatar
+ 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.
7th Jul 2018, 8:50 PM
John Wells
John Wells - avatar