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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With