0

I'm still a beginner at Arduino coding -- I've been researching on how to make functions using void instead of doing goto because reasons...

one thing I've noticed about void is that it has this void loop() {...} parenthesis, what is it for? (and can you provide an example of using it?)

say I make a void function void function1() to call the function I have to use function1(); can I make a function that loops? like the ol' void loop() {...} command we all know and love or do all void functions loop? if not then what happens to a void function after it reaches the end of its function?

3

The void in front of the function definition just tells what type the function returns. In the case of void it means the function returns nothing. If the function returned an int then it would be defined with int instead of void.

eg:

int addNumbers(int num1, int num2){
    int retval = num1 + num2;
    return retval;
}

int sum = addNumbers(3, 4);
Serial.print(sum);  // prints 7

No, the loop only loops because main (see main.cpp in the core) is the "real" program that is running on your micro and it calls loop over and over. It's not that the loop function on it's own repeats. It is literally being called repeatedly by the main function.

That example also gets to what the parenthesis are for. That's where the arguments that the function takes go. In the function definition you tell it what types they'll be and give them names. In the function call you put in whatever values you want to function to use. In the case of void loop(), it takes no arguments so the parenthesis are empty.

I would suggest you look at a basic tutorial on C++. There are many good ones on the web. The concept of functions isn't something that you can reason your way through by doing. Go read a little and you'll get it pretty quick.

  • Is there any way to run multiple void functions at the same time? – Dudamesh 192 May 24 '17 at 3:00
  • Not on an Arduino. It has a single core. It executes one instruction at a time. – Delta_G May 24 '17 at 3:03
  • PC's that are capable of multithreading can be running several instructions at the same time, so they could be running multiple functions at the same time. But Arduino can't do that. – Delta_G May 24 '17 at 3:04
  • 3
    Also note, they're just called functions. Not "void functions". The void is the return type. It just tells you what the function returns. – Delta_G May 24 '17 at 3:05

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.