fundamental question regarding declaring and calling the loop and setup function

I have been using functions for a while but one thing keeps confusing me; the loop and the setup functions. I know how to use them so this is more a fundamental question.

The question is basically: when I use the setup function, am I declaring the function, calling it, or both at the same time?
Let me elobarate with a piece of code

void setup() 
{
//initialize program
}

void myfunction ()
{
 //do some stuff when called
}

void loop() 
{
// do something all the time
}

based on the syntax, the code just looks like three functions are declared withouth actually calling them, but when running this program, it will actually do the stuff in setup() and in loop() but not the stuff in myfunction(). That makes it look like I am calling the functions loop() and setup(), however callig function would normally not have the void and the brackets. How this work? I hope my confusion makes any sense :).

When your code is compiled the IDE arranges for setup() to be called once and for loop() to be called repeatedly after that. You need to do nothing to make this work apart from writing the functions. Calling other functions that you write is up to you and the functions can appear in the code in any order.

There are, I believe, some circumstances where it is necessary for a function prototype to be present in the code before the actual function but this is not usually necessary.

I recall reading somewhere on the forum, that there's an "invisible" main() function that a non-arduino-C program would have, and it's in there that the setup() and loop() are kicked off.

You can find main.cpp in the sources included with the IDE.

Thanks for the posts! It makes a lot of sense now. I am declaring the loop and setup functions in my sketch and the IDE automatically calls them by using the main() function.

Leon_fromDelft:
Thanks for the posts! It makes a lot of sense now. I am declaring the loop and setup functions in my sketch and the IDE automatically calls them by using the main() function.

Correct

main() calls setup() once and then continuously calls loop() in a never ending for(;:wink: loop

Here's the Arduino main(), slightly stripped for clarity in the context of this thread

int main(void)
{
	init();
	setup();
	for (;;) {
		loop();
	}
	return 0;
}

Rob

I am declaring the loop and setup functions in my sketch and the IDE automatically calls them by using the main() function.

No. You are implementing the loop() and setup() functions. There is a function prototype that declares that such functions are going to be implemented later.

Leon_fromDelft:
Thanks for the posts! It makes a lot of sense now. I am declaring the loop and setup functions in my sketch and the IDE automatically calls them by using the main() function.

Well mostly right, except for the bit about "the IDE automatically calls them". That's not right. The IDE ( which is running on your computer ), does not call any part of your sketch executable ( which is running on your Arduino ).

Well mostly right, except for the bit about "the IDE automatically calls them". That's not right. The IDE ( which is running on your computer ), does not call any part of your sketch executable ( which is running on your Arduino ).

Ok that is right of course , what I mean is that the IDE adds code that calls the functions (secretly...).