I understand it can be confusing, but consider this: the compiler is straightforward and expects a function to return something when it’s called. If the function doesn’t return a value, you must specify void in the first part of the declaration. This tells the compiler that the function returns nothing.
Here’s a breakdown:
First Spot (Return Type):
This specifies what the function will return. It can be void (nothing), byte, int, or another data type.
Second Spot (Function Name):
This is the name you give the function so you can call it in your code.
Third Spot (Parameters):
The parentheses () define the parameters passed to the function. These can be:
Local: Declared within the function, e.g., (byte x).
Global: Accessed directly from anywhere in the program.
fourth Spot {code}:
This is where the code goes that performs your function.
Here’s a simple example:
void printMessage() { // 'void' means this function returns nothing
Serial.println("Hello, World!");
}
int addNumbers(int a, int b) { // Returns an int and takes two int parameters
return a + b;
}
void loop() {
printMessage(); // Calls the first function
int sum = addNumbers(5, 7); // Calls the second function and stores the result
Serial.println(sum); // Prints 12
}
For more clarity, here’s a helpful resource for understanding C++ functions. While it’s not entirely Arduino-specific, almost everything applies and will give you a solid foundation: https://www.youtube.com/c/CodeBeauty