4

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.

Majenko
  • 103,876
  • 5
  • 75
  • 133
Nick Gammon
  • 35,792
  • 12
  • 63
  • 121
  • 1
    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 08: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 09: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 09: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 09:39
  • 1
    None at all! (I made a factual error in my answer which I appreciated having Edgar Bonet correct). – Nick Gammon Mar 24 '18 at 09:47

2 Answers2

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;
    }
Edgar Bonet
  • 39,449
  • 4
  • 36
  • 72
Nick Gammon
  • 35,792
  • 12
  • 63
  • 121
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".

nPn
  • 129
  • 3
  • 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?](https://arduino.stackexchange.com/help/self-answer) and [It’s OK to Ask and Answer Your Own Questions](https://stackoverflow.blog/2011/07/01/its-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