Go Down

Topic: BEGINNERS: setup() loop() ‘void functions’ and scope.... (Read 847 times) previous topic - next topic

lastchancename

May 05, 2018, 07:38 am Last Edit: May 06, 2018, 05:57 am by lastchancename
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.
Ask the right question, which can be hard for beginners, but this doesn't mean that google is broken.
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

6v6gt

#1
May 05, 2018, 08:41 am Last Edit: May 11, 2018, 09:28 am by 6v6gt Reason: new link
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

https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/main.cpp

ChrisTenone

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.
I don't got to show you no stinkin' signature.

DrAzzy

I got a 404 error with that link.
Yeah, they just removed all those files from the repo, I don't know where to.
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

6v6gt


adwsystems

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.

Go Up