Are function prototypes needed in header files?

26,296

You should (almost) never put function definitions in header files, as you've done in your header1.h.

Header files should contain function declarations (prototypes).

(A "prototype" is a function declaration that specifies the types of the arguments. There are non-prototype function declarations that don't specify argument types, but they're obsolescent and there's no reason to use them.)

Function definitions (with the { ... } code that implements the function) should be in .c files.

Each .c file should have a #include directive for any functions that it calls or defines.

And each header file should be protected from multiple inclusion by include guards.

The idea is that each function declaration appears exactly once in each translation unit (each source file that you compile), and each function definition appears exactly once in your entire program.

If you have a function that's used in only one .c file, you can put its declaration and definition in the same .c file (and you should probably define it as static). In fact, if the definition appears before any calls, you can omit the separate declaration; the definition itself acts as a declaration.

(Functions defined as inline can complicate this model a bit; I suggest not worrying about that for now.)

Share:
26,296
Bennett Yeo
Author by

Bennett Yeo

Developer and Intermediate level computer programmer with an intermediate-advanced grasp of code architecture. Fluent in C#, Java, JavaScript, Hyper Text Markup Language, C and Java and Extensible Markup Language. I have also programmed lego robots throughout my preteen years and moved into tetrix robots and RobotC. I was the lead developer and 'computer systems analyst' within my First Technical Challenge team and developed a never-done-before first person control system interface for the wifi controllers. I currently attend the University of Wisconsin Madison programming a Vehicle Dynamics Simulator and other related Full Stack Development projects under Professors David Noyce and Kelvin Santiago for the Civil Engineering Department. "I am a program that converts caffine into code"

Updated on August 20, 2020

Comments

  • Bennett Yeo
    Bennett Yeo almost 4 years

    I am programming in robotc which is just c programming with add-ins (follows all c rules). In order to organize my code I have put my subroutines in header files and are referencing from my main c document. Can I still reference the methods in the header files from the c document without putting function prototypes in the headers?

    For example:

    Code in main1.c

        #include header1.h
        task main()
        {
          header_method();
        }
    

    Code in header1.h (no function prototypes)

       header_method()
       {
       //do stuffs
       }
    

    Or do I have to do this:

       void header_method();
    
       header_method()
       {
       //do stuffs
       }
    

    The reason is that I can only declare a certain amount of global variables for my robot in robotc.