0

Is there a reason why it might not be good practice to place C/C++ function prototypes in header files but instead place them at the top of the main .c/.cpp file?

For example, I could write a dothis.c file:

#include "funcs.h"

int main(int argc, char ** argv) {
   // some code
   int result = doThis();
   // more code

   return 0;
}

int doThis(void) {
   // code and return value
}

And in my funcs.h file, I'd write:

#ifndef FUNCS_H
#define FUNCS_H

int doThis(void);

// more function prototypes

#endif // FUNCS_H

Is there any reason why it might be better to place the prototype(s) (assuming there are many) at the top of the .c/.cpp file instead? For example:

#include "funcs.h"

int doThis(void);

// more function prototypes

int main(int argc, char ** argv) {
   // some code
   int result = doThis();
   // more code

   return 0;
}

int doThis(void) {
   // code and return value
}

In the first case, I feel it would be helpful to have many function prototypes in a separate header file plus documentation to logically separate declaration from implementation and to make it easier to concisely see what the main file is doing.

James Downer
  • 31
  • 1
  • 4
  • 4
    When the function is only to be used internally and is not part of the functionality exported/exposed by your header file. – Richard Critten Jun 06 '18 at 17:27
  • 1
    If your function is only used inside a single file, then it is perfectly valid to put it inside the cpp file itself. If your function is meant to be reused it's better to put it in header... – AlexG Jun 06 '18 at 17:28
  • Declare the internally used functions as static. Put the interface function declarations to header file. – Öö Tiib Jun 06 '18 at 17:28
  • 2
    Primarily opinion-based. – SergeyA Jun 06 '18 at 17:29
  • 1
    *Only* if the function is both defined and exclusively used in that file (in which case it might as well be explicitly declared `static`). But an `extern` prototype in a `.c` file, for a function defined elsewhere, pretty much demolishes the whole point of prototypes. (You weren't asking about `extern` prototypes, but I wanted to make this point. See also [this question](https://stackoverflow.com/questions/50401100/compiler-warning-for-function-defined-without-prototype-in-scope).) – Steve Summit Jun 06 '18 at 17:29
  • Opinion-based. I avoid prototypes for unexposed functions, preferring to implement the functions in an order where it is not necessary. I'm lazy. I can't think of any good enough reasons to have to make two changes when I'm forced to update the signature of a function that is only used internally. – user4581301 Jun 06 '18 at 18:23
  • @user4581301 Yeah, me too. I bet I'm even lazier than you. – Paul Sanders Jun 06 '18 at 19:40

1 Answers1

2

Is there a reason why it might not be good practice to place C/C++ function prototypes in header files but instead place them at the top of the main .c/.cpp file?

The only time that makes sense to me is when the functions are implementation details of other functions. Of course, in that case, I prefer to define them in the file scope using file scoped functions using static qualifier or putting them in a namespace specific to the .cpp file.

For all other functions, I find it hard to justify not putting the declaration in a header file and #includeing the header file in the files that use the functions and in the file that defines the functions.

R Sahu
  • 204,454
  • 14
  • 159
  • 270