I am looking at some embedded firmware in C language. I notice there are static functions being used in .c files but there are no function prototypes. Is it a good practice to always put function prototypes near the top of the .c file? Are there situations when not putting function prototypes would be better?
-
1Well, I generally do not write function prototypes. The ones I write are in *header* files, or if I have mutually recursive functions. – Bakuriu Jul 28 '16 at 08:46
-
1Excluding the scenarios mentioned in this [\[ pro-prototype (or is it? ) \]](http://stackoverflow.com/a/21670762/1620779) answer, you may not put prototypes if you're tired of typing. – sjsam Jul 28 '16 at 08:53
3 Answers
Is it a good practice to always put function prototypes near the top of the .c file
no. If using multiple .c files that are calling the same function, it would be better to declare that function in a .h file and include it, instead of redeclaring it in every new .c file that calls the function
If using only one file, and you are not sure whether to declare it or not - declaring would enable you to call the function before its definition, so I think it would be good practice
Are there situations when not putting function prototypes would be better?
there are times in which you can compile without prototypes, but in general I think it's not better to omit them, but I don't think I can say it's really really bad

- 12,097
- 11
- 59
- 124
-
2I think you missed the question addresses static functions, as stated in the title. Static functions are not visible in other translations units (http://stackoverflow.com/questions/558122/what-is-a-static-function) hence the question of putting the prototype at the top of the .c file. – Heyji Jul 28 '16 at 09:18
-
1@Heyji I'm not sure. In the body of the question, the OP says **always** so I answered to that. I admit that for static functions, this reason does not hold. Nevertheless, I would argue it is much clearer and better written, if declarations would stay in the h file – CIsForCookies Jul 28 '16 at 09:59
-
1There is not point of putting the prototype of a static function in a header file, as this function would never be seen in other translation units. Moreover, even if the OP wrote **always**, this always was still refering to static functions only, otherwise he would not have mentionned .c files. – Heyji Jul 28 '16 at 10:05
Note that making a c function static hides it to other translation units...
I see two reasons for providing a prototype of a static function at the top of the .c file:
- You need to use that function before it is defined.
- You want to ease readability by gathering all local functions' prototype at the top of the file and provide an overview of what is available in the file (only locally).
The second argument is probably arguable since there are many ways to discover/read source code nowadays. So I don't think it should be a practice. You do it only when needed.

- 1,113
- 8
- 26
The main use for function prototypes is in header files. They are also handy if you define mutually recursive functions. Other than that function prototypes just lead to unnecessary code (declaration) duplication.

- 10,773
- 7
- 38
- 60