0

I hope this is a simple request from a newcomer to Arduino and programming. I have understood that once a program enters a void loop section, it loops round indefinitely. However I have recently seen examples of sketches where another void $$$$$ section follows the closure (by}) of the void loop. As I write this, I am looking at one example concerning a 7 segment display where "void Num_Write(int)" follows the closure of the void loop. If the "loop" section does just that, as I have been lead to believe, how is a following section accessed?

Thank you, Rob

1

The void loop() section is a function, the same as void setup() is a function. When you start you Arduino program another function, that you can't see in the IDE, call setup() and then calls loop() repeatedly.

These other function you have seen, i.e. void Num_Write(int n) can be called from either setup() or loop() and they will execute their instructions before returning to the function that called them. Its also possible the Num_Write() may call other functions before it returns controller to the function that called it.

The type void is used to represent nothing (its actually more complex than that, but for this level of explanation nothing will do). Other functions may return other types int, char, bool for instance. So if we had a function like this:

int theMeaningOfLife(void)
{
  return 42;
}

This is a function that returns an int (a number) so you would call it like this:

void setup()
{
  int number = theMeaningOfLife();
}

There is one catch, the function has to be declared BEFORE it is used, this allows the compiler to know what you are referring to. The simplest way of doing this is to write the function above the function that is calling it. The second way is to just write the function's signature above the function using it, i.e.:

int theMeaningOfLife(void);

void setup()
{
  int number = theMeaningOfLife();
}

The best way (but not always practical on an Arduino) is to have all the function declarations in a header file (.h) and the code in a .cpp file. (Have a look a library to see what I mean).

Summary

There is only one function that is called repeatedly and that is the loop function. Just because a function returns void doesn't mean its going to loop repeatedly.

  • You wrote: “the function has to be declared BEFORE it is used”. Actually, the Arduino IDE runs your code through a pre-processing step that does that for you, meaning you do not have to do it yourself. I still would consider good practice to stick to this rule, if only because it makes the code compilable outside the IDE, e.g. with a Makefile. – Edgar Bonet Nov 7 '16 at 13:34
  • @EdgarBonet - Thanks, I never knew that. I think I should forget it though, otherwise I'll start trying it at work and end up pulling my hair out trying to work out why [it] won't compile. :) – Code Gorilla Nov 7 '16 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.