BEGINNERS: setup() loop() ‘void functions’ and scope

Thi starter has been copied from an answer I posted in another topic...

Let's step back a bit from Arduino...

'C' and it's peers expect a main() function, but this main() has been hidden by the Arduino IDE to make separate setup() and loop() functions that may be easier for beginners to grasp.

VOID...
When you see void placed before a function() name, it simply tells the compiler that particular function doesn’t return any type of value to whomever ‘called’ it.

If you see VOID in the function(parameters). void function(void) {}
That simply means the function doesn’t expect any incoming values.

(There are several ways around this, but that’s beyond the ‘scope’ of this discussion.)

setup() is only ever executed once - but you can do almost whatever you want inside it... even 'looping' ! Your code could avoid the actual loop() function forever if you wanted to...!

In most sketches, coders use the 'one time' setup() function to initialise the environment for their program, and then proceed on to loop() to execute the body of the program.

Arduino doesn't give you any practical way to 'exit', ‘return’, or get out of loop(), so your program effectively runs forever unless you specifically derail it.

Now, beyond setup() and loop(), you may create your own function()s - just like setup() and loop() to accept and return values from any part of the program that 'calls' them.

These function definitions contain a return type, function name ( input variable list) - followed by one or more lines; of code to be executed within the function call.
e.g.
void myFunction(int aNumber) { code; }

SCOPE
Just to make it worse, any variables you toss around and manipulate throughout your program have scope, which determines when they exist, and contain valid values. Scope may be global, local to a function, or local to any { block of code }
If you access a variable outside of it's valid scope, you'll get a compiler error.

(Functions declared in another module may also be ‘out of scope’, but that’s a relatively advanced concept for this thread)

Ok, so there’s the real basics of a programs structure.
Unfortunately, no two projects are the same, even if they use the same hardware to achieve the same outcome.

There are NO rules HOW to write ‘sketches’, or code - but there are plenty of rules how NOT to write code. The compiler often catches these!

There are usually three or more ways to approach each individual question & answer when programming...
If you think of that as [each function x 3 methods], and [you have 10 functions] in your code, [factorial that together], and you have a boatload of perfectly legitimate strategies to address the same question and get the same answer!

About the only two coding examples I can think of with maybe TEN possibilities each are - ‘Blink a LED’, and ‘Hello World’.
The problem grows exponentially, and the skill is not only knowing your tools, but when to use each one.

The nitty-gritty will be provided somewhere else.

For those interested, and did not already know, here is the Arduino main() function where both setup() and loop() are defined:

~~https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/main.cpp~~

new link @ 11. May 2018

6v6gt:
For those interested, and did not already know, here is the Arduino main() function where both setup() and loop() are defined:

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/main.cpp

I got a 404 error with that link.

ChrisTenone:
I got a 404 error with that link.

Yeah, they just removed all those files from the repo, I don't know where to.

Link to main() updated for new location.

lastchancename:
setup() is only ever executed once - but you can do almost whatever you want inside it... even 'looping' ! Your code could avoid the actual loop() function forever if you wanted to...!

Be warned that if you do, then then you cannot use serialEvent() as it is in the same for loop that contains the call too loop().

As loop() is a subroutine, the variable declared in loop() are reset on each call or pass through loop(); unless declared as static.