Seperating functions into modules

Hi,

I have a code in which the user inputs 3 values. Area and volume are then calculated. I’ve separated the code for these calculations into their own modules, which I’m just learning about how to use.

I used the serial monitor for printing in each of the modules, and I read that #include "Arduino.h"<\b> is required in each of them:
source

Questions:

  1. I’m new to the modules, so I don’t know what an API is (and separately, not into classes yet). Can someone explain how this #include "Arduino.h" is being useful?

  2. I’ve used the file extension .cpp for the modules, but when I tried using .h, the code won’t compile. What’s the difference between the extensions, and why is the Arduino.h allowable?

Note: I've attached the main source file and its modules, which I think are called compilation units (I'm such a noob.)

math_formulas_user_entry_multiple_files_4.ino (5.92 KB)

area_calculation.cpp (441 Bytes)

volume.cpp (459 Bytes)

a .ino file is converted into a .cpp file by adding other statements, including "#include "Arduino.h" which has many of the declarations for arduino "built-in" functions. look at Arduino.h under

Arduino/hardware/arduino/avr/cores/arduino/Arduino.h

when you create a .cpp file that used Arduino built-in functions and constants, you need to include Arduino.h in the .cpp file

when you create a function in a .cpp file that is called from another file, the compiler needs information about that function when compiling the other file. that information is the return type and argument types of the function. a .h file typically has a "declaration" of the function "defined" in the .cpp file for example "int myFunc (const char* s, int len);"

The technique I prefer for creating a multi-file project is described in My Reply #3 Here.

your function prototypes should be a in a .h file for the .cpp file defining the function

"extern" declares a variable defined external to the file (in another file). you could put extern variables declarations in a .h for the file defining the variables. but it is "bad practice" to use externs the way you are using them.

Thanks for your responses! I want to make sure I got this so please correct me if I’m wrong. (I've updated my files, as attached.)

The main source file - default when opening a new program which contains the setup() and loop(), the .ino, is converted to a cpp file when compiled.

Each new tab is a module, with extension “.h”, which the source file calls, e.g. “#include area.h”

Two questions

  1. I only need to use #include Arduino.h in the modules. In my example, I call Serial.begin(9600); in the setup() of the source file (or as gfvalvo refers to it as the .cpp file), and thus this is needed to “tunnel”

This confused me, since on gfvalvo's line,

when you create a .cpp file that used Arduino built-in functions and constants, you need to include Arduino.h in the .cpp file

...it appears I had to use this in my source file, but I actually didn’t have to as it was sufficient to only use “Serial.begin(9600)” in the setup.

  1. What is meant by #include "guards”?

I think I get these:
3. I was redundantly including function prototypes both in the source file and the modules I switched to only using the prototypes in the .h file where its function is defined, per gcrj’s comment. (seems ok)

  1. Terminology: #include "Arduino.h" is the API which allows the module to use the built-in function of the Serial library, which is defined in Arduino’s core libraries. However, this only works since Serial.begin is declared in the source file (and declaring it again in a module is bad programming practice).

math_formulas_user_entry_multiple_files_5.ino (5.9 KB)

area_calculation.cpp (441 Bytes)

volume.cpp (459 Bytes)

here is what your .cpp with all equations might be, eq.cpp (with area and vol defined within the funcitons

#include "eq.h"

// area of a Rectangle
int areaRectangle(int side_1, int side_2)
{
    int area = side_1 * side_2;
    Serial.print("area of rectangle = ");
    Serial.println(area);
    return area;
}


// volume of a cube
int volumeCube(int side_1, int side_2, int side_3)
{
    int volume = side_1 * side_2 * side_3;
    Serial.print("volume of cube = ");
    Serial.println(volume);
    return volume;
}

the corresponding eq.h (with guards to prevent it from being included more than once)

#ifndef EQ_H
# define EQ_H

int areaRectangle(int, int);
int volumeCube(int, int, int);

#endif

and how eq.h and eq.cpp might be used from a separate file (main.cpp tested on laptop. i put #if 0 around you Serial prints when testing. )

#include <stdio.h>

#include "eq.h"

int
main ()
{
    printf (" area 2x3 %d\n", areaRectangle (2,3));
    printf (" vol  2x3x4 %d\n", volumeCube (2,3, 4));
}

you would need to include "Arduino.h" if you call Serial.println()