Functions and their paths?

As I don't know what the correct question is I can't Google the answer. I'm using ESP8266, I'm trying to understand how the 'flow' of functions and Loop works?

I come form a background in programming where there isn't loops, just functions and procedures (my background is Delphi, Pascal ), however there is not easy way to see the flow (in Delphi I can step in and follow the flow line by line

So here is the question,

On bootup, void setup() gets run, then the void loop() starts.

Let say I have 5 functions and my loop

loop() {
  func1()
  func3()
}

void  func1 () {
  func2 ()
}
void  func2 () {
  func4 ()
}
void  func3 () {
  func5 ()
}
void  func4 () {
  
}
void  func5 () {

}

The way I would view the 'flow' that the functions get called and run, once they have run they return

So in the above, the Loop runs func1, I would see the flow as

func1 > func2 > func4, once func4 has finished it go's back to func2 < func1, func1 returns to the loop, then the loop calls func3, which calls func5, func5 returns to func4, then back to the loop where we start all over again.

If this correct?

Reagrds

Brian

Essentially, yes. What really happens, is when the code is compiled it is processed line by line starting with line1 which equates to the top of your code as viewed on the screen or printed out. Processing literally falls through your code from top to bottom.

func5 returns to func3 (I assume you mistyped).

In a grand scheme, everything is executed sequentially because that's the only thing the processor is capable of.

What isn't clear, is the actual code that gets compiled in the background which is,

int main(void)
{
setup();

while(1)
{
loop();
}
}

Yes

loop() is also a function and is called repeatedly (in a for-loop) from main().

setup(: is also called from main(), but only once.

Every C/C++ has a main() function which is the starting point of your code. In the Arduino world it's hidden from you but if you search the installation directory, you can find it.

// edit, it might be a while-loop instead of a for-loop; as indicated above.

Functions are resolved as subroutine call instructions within which is a RET instruction that restores the PC to the location of the function call +1.

ukeventlighting:
As I don't know what the correct question is I can't Google the answer. I'm using ESP8266, I'm trying to understand how the 'flow' of functions and Loop works?

I come form a background in programming where there isn't loops, just functions and procedures (my background is Delphi, Pascal ), however there is not easy way to see the flow (in Delphi I can step in and follow the flow line by line

setup() and loop() are specific to the Arduino core used by most Arduino products and many clones and derivatives. The language of Arduino is C++, which is an evolution of C. All C programs "start" at main(), which is found in main.cpp, which is part of the core. If you follow the link and read the source code for main.cpp, you will see where the function calls to setup() and loop() are. Just as these two functions are called from main, your functions can also be called from almost anywhere in your code. In fact, most of the Arduino language is function calls to parts of the core. Any functions written in your code, in a library or in the core, that are not called from somewhere in your program flow, are ignored by the compiler.

Processing literally falls through your code from top to bottom.

I would not say that.
The following is a perfectly valid sketch, setup() is still executed first and anotherFunction() is never executed at all.

void loop() {
   Serial.print("test");
   delay(1000);
}
void anotherFunction() {
  Serial.print("Nobody expected that!");
}
void setup() {
  Serial.begin(9600);
}

my background is Delphi, Pascal

A quick look seems to indicate that Delphi is very centered around "GUI Events" - you write a bunch of functions and procedures that are attached (by the compiler and run-time environment) to forms and windows that you create with a GUI editor, and they are "called" when those events occur. The program starts up and automatically creates the windows/buttons/etc that you defined, and then when an event occurs (the user clicks a button), the appropriate procedure or function is executed. Is that about right?
In C, "procedures" and "functions" are defined the same way - what would be a procedure in Pascal is just a function with a "void" return type in C. (so "void setup()..." defines a procedure called "setup") You can think of setup() as the procedure that handles the "reset" event, and loop() is the "default event" that is simply executed over and over again... setup and loop are not "language elements", they're just procedure names...

There is absolutely ZERO difference between the way Pascal and Delphi execute code, and the way c++/Arduino executes code, other than the mountain of stuff that Delphi does in the background to do all the GUI event handling. But, if you write a Delphi program that run ONLY your code, and does not use the GUI and other Delphi-specific features, and translated that program to c++ to run on the Arduino, the flow of execution would be identical.

As indicated by others, Arduino DOES have a main, but it is created automatically, and hidden from the user. But, if you want, you can provide your own main, and it will over-ride the built-in one. You just need to make sure you call the Arduino init() function as the first thing in your main, so all the Arduino stuff (like Serial, etc.) gets initialized properly.

Regards,
Ray L.

Sorry for my delay in replying, busy weekend, and have been chasing the fault I have which was the reason I asked the question to start with.

Thanks for all your replies, the flow is what I thought, I really couldn’t see functions being that different.

arduino_new:
func5 returns to func3 (I assume you mistyped).

Opps yes sorry

The reason for the question was I'm getting straight results from things, I know I'm I am new to the world of Arduino and it’s a very large learning curve but something its right.

I'm working on a project using WS2811 running on a ESP8266, the WS pixels are very picky about timing, however if I have got my info right (and there is no guarantee I have) this shouldn’t a be problem

To make life easier for programming I have put various functions in .h files. One of the functions runs the WS2811 output

The full code from the project is far to big to post but here is a break down of what's happening.

I have a .h file with the following

#ifndef JSON_WS.h 
#define JSON_ WS.h

void ICACHE_FLASH_ATTR WSOut() {
	os_intr_lock();
  	for (uint16_t t = 0; t < 512; t++) {
		.. process the data and send them out, there are timed loop etc to keep the timings using the pixArray[t]
	 }
	os_intr_unlock();
}
#endif

// main file

uint8_t pixArray[512];
#include "WS.h";

void loop() {
	 webSocket.loop();
	server.handleClient();
	// other timers via millis and doing stuff

	if (udp.parsePacket()) {
		
		// check the Packets, process into pixArray
		WSOut ();
}

So based on the above this is what I should expect to happen

It just sit in the loop doing the websocket and server and checking for data, if data is found, run the WSOut.
If data found, it should go off a run the WSOut, it shouldn’t return to the main loop until it has finished processing the data in WSout.

The WSout is stored in the .h file, in this function it calls os_intr_lock(), os_intr_unlock(); I can't find documentation to these, but have found references. From what I understand this said, "I don't care what else you are doing, stop it, and ONLY deal with this"

The issue I have is that I get the 1st LED flashing green, this is where I have spent hours chasing my tail, it turns out that is I remove the WSOut function from the .h file and keep it in the main file, it works.

This makes no sense, why would the code in the .h file mess up, apart from it being stored in a separate file its the same code?

The idea of using .h files so store functions is basic stuff, most programmers will split functions out once they are working.

BRAIN HURTS!