Help understanding the excution flow withing a sketch

I am trying to wrap my head around the flow of a sketch and the flow within the sketch.

I think I am good with the fact that all before void setup() will excute first with void setup () next.

Now I get a little bit uncertain what happens next Lets say I have void a (), void b (), and last void loop (), last. Does the flow jump down to void loop ()? and skip over voids a & b? where as I need to make a call to void a() or void b()?

And what if you have a function after the void loop? and from some readings, is there a difference between a loop or main function? I am trying to build a sketch that has a need to scan in some info. Allow a user to make a selection and run a test that includes time and analog value then print out on a thermo printer and store on a uSD card test results. And thus I am going to have quite a few functions. Just not clear how the flow the program runs through the sketch and if someone can point me to a document that explains this, would be very helpful. I am one of those people who learn best via hands on. I suspect I will burn out the eeprom before I am done. LOL
Thanks
Paul

loop( ) will run over and over again. Any other functions that you put in your sketch will only be called if there are function calls made to them, from the code inside setup() and loop()

When you click the Compile icon, the IDE first invokes the C preprocessor which handles the “#” directives (e.g., #include, #define, #ifdef, etc.) Next it processes those data definitions that are not defined within a function. setup() comes next, and finally loop(). If you define two functions void a() and void b(), if setup() calls b(), as in:

#include <Wire.h>
int val;

void a() {
   Serial.println("I'm in a()");
}

void b() {
   Serial.println("I'm in b()");
}


void setup() {
   Serial.begin(9600);
   b();
}

void loop() 
{
   a();
}

the code that’s generated by the compiler calls b() first, since setup() is executed before loop(). Therefore, the output from above would look like:

I’m in b()
I’m in a()
I’m in a()
I’m in a()
I’m in a()

and so on. Note that the placement of the source code for a() and b() does not impact when they are called. If you ever need to determine the sequence, just put some print statements in your code.

Paul1958:
I think I am good with the fact that all before void setup() will excute first with void setup () next.

What? No.

Anything that's a variable initailzation

int x = 5;

executes first. Then setup(). Then the system spams loop() forever.

The order in which you create the functions within your sketch does not matter.

The code that you write is not the whole program. The Arduino IDE adds some more code behind the scenes. That “background code” runs first when the Arduino is restarted and, basically it has two jobs. It calls setup() once and then it calls loop() over and over again for ever.

That means that the first part of your code that runs is whatever is in setup() and when that finishes the code you have in loop() will start.

The code you write (whether in setup() or in loop() ) can call any of the other functions that you have created.

The code in planning and implementing a program is more extensive than many other examples and may illustrate the process more clearly.

…R

Sorry for the delay. Seems that IE on the work computers wont let me log in. I want to thank each one of you for you all gave me good pointers that help to fully explain the plumbing and how it flows through the sketch. Did wonders to clear up some matters that I just had no idea. econjack your explanation greatly cleared up that aspect.

I really appreciate all of the help. makes it easier to program and debug knowing how this works.

Paul1958:
I am trying to wrap my head around the flow of a sketch and the flow within the sketch.

I think I am good with the fact that all before void setup() will excute first with void setup () next.

Here’s what’s inside:

int main (void) <-- standard c type main
{
    init(); <-- small piece of code sets up timers & such
    initVariant(); <-- "hook" or back door for 3rd party board variants
#if defined(USBCON)
    USBDevice.attach(); <-- for USB based AVR like Leonardo
#endif
    setup(); <-- runs your code that's in "setup()"
    for (;;) { <-- forever.....
        loop(); ....run what's inside your "loop()"
        if (serialEventRun) {
            serialEventRun(); <-- asynchronous serial event hook
        }
    }
    return 0;
}

So you see, all you REALLY need is this:

int main (void)
{
    // declare your variables here
    uint8_t x, y, z;
    uint16_t a, b, c; // etc..

    init (); // gotta have this

    // put in here whatever runs once (setup, pinMode, Serial.begin, etc..)

    while (1) {
        // put any code you want tun forever in here
        // (i.e. this is the same as "loop()")
    }
}

Personally, I prefer to program like above. I don’t use the setup and loop thing.