5

I have posted questions about "my void loop" or "my void setup" and people complain that they aren't really voids. But they are! See this example code:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Clearly setup and loop are voids because they have the word void right before them!


Note: this is a reference question.

  • Sorry Nick, but this reference question looks more like a general C++ prgramming question (belonging to StackOverflow then) than an Arduino question, except for the actual functions content. Some members might well vote to close this kind of question as "off topic". – jfpoilpret Mar 24 '18 at 8:06
  • Yes I realize that, but Arduino users tend to be beginners to programming in general and C++ in particular. The constant references to "the Arduino language" makes me think that they might not consider checking StackOverflow for an answer. Indeed, it is addressing a problem that beginners might not even realize exists (terminology). By posting this Q&A I am attempting to provide a reference point for people who talk about their "void loop" problem. – Nick Gammon Mar 24 '18 at 9:21
  • Don't get me wrong, I'm not insulting people who don't know C++. There was a time when I didn't have the faintest idea about C or C++. Previously I had been using Turbo Pascal, and was used to procedure and function declarations, and thought that C had very weird syntax. And before Turbo Pascal, well that's a whole other story. :) – Nick Gammon Mar 24 '18 at 9:33
  • Don't worry Nick, no risk I get you wrong, you are one of my electronics rock stars (I really appreciate your posts about electronics on your site)! I agree with the fact that many Arduino newcomers will first come here for a C++ question. My comment was more about how some Arduino.SE members (including myself) might react on such a question and might consider it off-topic. No hard feelings! – jfpoilpret Mar 24 '18 at 9:39
  • None at all! (I made a factual error in my answer which I appreciated having Edgar Bonet correct). – Nick Gammon Mar 24 '18 at 9:47
7

This is an easy mistake for a beginner to C++ to make. Let's look at other languages, like PHP:

function add ($a, $b)
  {
  return $a + $b;
  }

Or Lua:

function add (a, b)
  return a + b
end

Or JavaScript:

function add (a, b)
  {
  return a + b;
  }

Or VBscript:

Function add (a, b)
  add = a + b
End Function

All those languages have the word "function" there to indicate that you are declaring a function.


So what does C++ look like:

int add (int a, int b)
  {
  return a + b;
  }

So this must be an "int" right? Or what if it doesn't return a value?

void loop ()
  {
  }

Now it's a "void"? Kind of a weird name for a function!

Actually int and void in these examples are not weird names for functions. They are the return types of those functions. int add (int a, int b) is a function that returns an int. And void loop () is a function that returns nothing.

The C++ compiler can deduce when it is seeing a function declaration from some clues:

  • A return type (eg. int, float, long)
  • A name (the function name)
  • Some parentheses (for the function arguments)

Think of the word function as being there in spirit:

int [function] add (int a, int b)
    ^^^^^^^^^^   <-- don't actually type this!
    {
    return a + b;
    }
  • sorry, that is not Java in the code box after Java label. Java has voids :-) – Juraj Mar 23 '18 at 5:53
  • Changed it to read JavaScript. :) – Nick Gammon Mar 23 '18 at 6:05
0

I think if you use a phrase like " my int a or my CustomType a " people would interpret those phrases to mean that you have a variable a that is an instance of an int or a CustomType.

According to http://en.cppreference.com/w/cpp/language/types you can't have an object or reference of type void.

If you have a phrase like "my void setup", I think most people would try to read that as implying you have a variable setup of type void, which I don't think makes sense, to most people.

What you actually have is this:

void setup() {
...
}

I think most people would phrase this as "my function setup, which takes no arguments are returns nothing".

  • you missed the "Note"? – Juraj Mar 23 '18 at 18:28
  • I guess I did, and I later noticed that the same person answered it as asked it, which seemed weird. I have never seen or heard of such a thing as a "reference question, at least not on stackoverflow, is it documented somewhere? I searched the meta site and did not find it. – nPn Mar 23 '18 at 20:05
  • See Can I answer my own question? and It’s OK to Ask and Answer Your Own Questions. From that: it is not merely OK to ask and answer your own question, it is explicitly encouraged. – Nick Gammon Mar 23 '18 at 21:04
  • Thanks, I guess I have seen cases were people answer their own question, after the find a solution, but not this type of case. I can see cases were it would be a value add. But I guess then I don't see the point of someone pointing out the fact that it is a "reference" question, after all a question is a question and someone could still have a better answer than the person that asked it. – nPn Mar 23 '18 at 21:34
  • You are certainly welcome to post a better answer. However by not disclosing that it is a reference question people might wonder why someone asks what seems to be a simple question, and then answers it themself. The first time I did that I got complaints along those lines. :) – Nick Gammon Mar 24 '18 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.