Declaration of Functions

Guys, let's say I have the program in which I have 3 functions inside the loop. Do I need to declare these functions before setup?

no!
of course, I think you mean function calls, not function declarations.

That is an invalid code structure. loop() is also a function. Functions can't be inside of other functions.

Using the Arduino IDE, you (most of the time) do not have to declare a function before it is used. The IDE automatically generates function prototypes and places them at the beginning of the code.
(I say most of the time, because the automation generation mechanism occasionally does not work properly, particularly when dealing with something like an enum that needs to be declared before the function prototype).

I think OP means prototypes. The answer is the same.

1 Like

To improve my question follow example

void setup () {
...
}

void loop () {
...
LedOn();
LedOff();
LedBlinks():
...
}

void LedOn(){
...
}


void LedOn(){
...
}


boolean Blinks(){
...
}

Do I need to declare these functions before void setup
void LedOn();
void LedOff();
boolean LedBlinks():

No, although you may if you wish. The C++ compiler will need those prototypes but the Arduino IDE takes care of them for you. Usually.

In a .ino file no. The Arduino IDE will put them there for you.

Anywhere else the answer is yes.

1 Like

DO you need to?
No, while it is true that they must be there. Arduino takes care of that for you.....most of the time.

Can you and should you?
Yes, Arduino doesn't always get it right and that can create a problem that needs to be solved. If you always declare the prototypes yourself, you eliminate that issue before it arises. I personally also like having a list of functions at the top for my own reference.

Great answers, as I come from assembly and now it's my first contact with C, C++ and good to know these important details.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.