Go Down

Topic: Placing all function definition in Setup() (Read 96 times) previous topic - next topic


As I'm new to programming I have a general structuring question. 

Would it be alright to place all function, variable, and constant definitions, in the Setup() function (making sure nothing was turned on/off by the end on setup).  The reason I want to do this is personally it helps me streamline the flow of my Loop() function.  Put another way its easier for me to read what my loop function is doing. 

Are there draw backs to doing this?/ Should I not do this?


No, sorry.

You can't define functions inside other functions - they have to go on their own.

Then if you define variables and constants inside setup(), their scope will be limited to the setup() function - nothing in loop() will be able to access them.

You could make them global (define them outside loop() and setup() ) but that has drawbacks (it can make your sketch hang onto memory unnecessarily).

The wisest way is to define variables so they have only the scope they need and no more.

If it needs to be in loop(), define it in loop()

If it needs to be in some small sub-block of loop (like the counter in a for statement etc) then define it inside there instead.

Limit the scope as much as you can so the processor can have that memory back when that small block is finished.


Oh ok that makes sense.
Thanks for the info

Go Up