2

Can i create a function inside of void setup() to recall in loop() function

asked May 22, 2017 at 16:14

4 Answers 4

4

No. You do not create functions within functions.

You can create a function, and then call it both from setup() and loop() if you so wish.

answered May 22, 2017 at 16:19
1

Defining a function within a function is not supported in C or C++. If it were supported in a way consistent with the rest of the language, the inner function would be local to the outer function, just as local variables are, so not callable or even visible from outside the outer function.

Your loop() function (or any other function) could, however, call the setup() function as long as you want to execute setup() again in its entirety. That means that any initialization you perform in setup() would be repeated each time you call it.

answered May 22, 2017 at 16:48
0

You can create functions within a function if the particular c compiler supports it - gcc, the compiler used by arduino, does. But not all c compilers support that.

Whether you can create a function within a function that recalls loop() depends on your meaning of "recall".

answered May 22, 2017 at 16:28
2
  • 2
    Can you cite a source or show an example? Local functions aren't part of the C or C++ languages (excepting private member-functions of a class). They would be very useful in a few situations. Commented May 22, 2017 at 17:02
  • @JRobert: gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html Commented May 23, 2017 at 10:11
0

You have asked two questions here. In the title, can you define a function inside of setup(): Yes.

But that is the "scope" of where it can be used.

In Loop() it won't exist, ("xxx undefined in this scope") so the answer to what you ask in the text is No.

answered May 23, 2017 at 7:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.