Understanding .h file includes

When using C or C++ the compiler needs to know that a function exists, what its return type is and the parameters that it needs to be passed before it can be used in a program

In a sketch you can do this either by defining the whole function before the function is called, usually at the start of the sketch, or by having a function prototype such as the ones that you quote at the start of the sketch even if the actual function definition is later in the sketch

However, to make it easier for beginners the Arduino environment does not actually require the function prototype to exist at all and will add them to the sketch for you as part of the compilation process. Hence, you can write a sketch that uses a function that you write and place at the end of the sketch and not worry about a prototype

However, that is not the case when you #include a library and the function prototypes must be supplied. This is one purpose of the .h file and the compiler then expects to find the corresponding .cpp file in the same folder as the .h file in order to actually use the code in the function

1 Like