I have done a lot of research on this, looked at many explanations relating to both Arduino and C++. I've looked at text explanations and video tutorials both. Read over the Arduino.cc reference pages. I've searched this forum and others and read over lots of explanations.
I would like some feedback on whether my understanding of the 'void' data type is correct, so please bear with me as I explain it, and please give me as much explanatory information on this, in order to fully flesh out my understanding of it. If you need to explain it with different examples, please do - there is no such thing as too much information in helping me to understand. Thanks.
Here's my current understanding:
• void is a data-type. Along the same lines as int or float or boolean. You use it to declare a variable or structure (eg. myCustom) as having the data type of void.
• the void data type eg.
void myCustom()
has no value. Not a value of 0, since that technically IS a value, but rather it is devoid of any value.
• why would I use a variable (or structure) that is devoid of a value? This is because we are interested in what is contained within the variable or structure. eg.
void loop() {
myVariable = 9;
myVariable = 10;
}
We don't care about loop per se, just the variables or anything else we have inside the loop structure.
• you wouldn't use void on a variable and then give it a value eg.
void myCustom = 10
• void is used on both setup() and loop() since the purpose of both of these is not to give any value to any other part of the code. Setup is just the structure in which we put things that we want to occur during the startup phase of the Arduino, we don't want 'setup' itself to have a value, just to run the things it contains. Loop will be devoid of value since we are only using it for it's ability to loop a chunk of code. It's usefulness lies in it being a looping container for other code. Having them both set to void, means that the program isn't expecting some sort of value from loop() and setup() and thus confusing it when it finds that they have never been given one.
I think that's all of my understanding of it at the moment.
Please correct me, re-explain it, give examples etc... I'm interested in fully understanding how it works and when it's used.
Are there other times it's used that I might not be aware of?
Thanks.