Used Void and then did not use Void? I don't understand why?

Why do I have "Void Main Colors" and then just "Main Colors". Is void necessary or just a good habit??

void setup()
{

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}


void loop()
{

  mainColors();

  showSpectrum();
}

void mainColors()
{
void mainColors()
{
}

is a function definition.

mainColors();

is a function call. It calls the function.


Note
Why do you use bold text? A lot of people don't like it because it's considered shouting.

In this case it does not add anything to your question either; use it sparingly to emphasize something.

1 Like

In the function definition, the keyword "void" preceding the function name indicates that the function does not return a value.

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:

  1. First Spot (Return Type):
    This specifies what the function will return. It can be void (nothing), byte, int, or another data type.
  2. Second Spot (Function Name):
    This is the name you give the function so you can call it in your code.
  3. 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.
  4. 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

Thank you for your detailed explanation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.