Void Setup

I've read tutorials here and on adafruit as well as elswhere , but can not find a detailed beginners explanation for the word VOID in void setup , and void loop , other that ' it does not return a value'.

I understand so far that Void setup needs to be at the beginning of a program , along with void loop , and have had some success writing and or modding a few sketches so far, but could use some help.

....'doesn't return a value' ..... from where exactly, and what kind of value ,and where exactly would it be be returning to ?

Thanks....

A Good book on C++ Might help... "Void" - C++ Search results.. Is Free Too...
This might help.. What value is setup() supposed to return and to where...
Setup() is called only once.
Void indicated that setup returns nothing.
http://www.cplusplus.com/ is one of Many "Go To" resources available for learning the language.

Doc

The questions you're asking can't be answered without raising more questions and confusion, given your level of knowledge. You should look at understanding what a function is, how they're used, their purpose, etc.

there are two type of functions, one that returns a value and one that doesn't. By return, it means when you call the function it returns a value like so.

void loop() {
      myValue = multiplyFunction(4,5);  // myValue will equal 20
}

int multiplyFunction(int x,int y) {    // x & y are declared as integers. 
     int result = x * y;               // x = 4 and y = 5 are passed into the function in the parameters
     return result;                    //returns the value where it is called
}

Notice the int datatype is in place of the void. That means this function is returning an integer.
When you write a function that doesn't return a value, you put the word void in front. So that's why void is used in front of loop() and setup()/

mistergreen:
there are two type of functions, one that returns a value and one that doesn't. By return, it means when you call the function it returns a value like so.

void loop() {

myValue = multiplyFunction(4,5);  // myValue will equal 20
}

int multiplyFunction(int x,int y) {    // x & y are declared as integers.
     int result = x * y;               // x = 4 and y = 5 are passed into the function in the parameters
     return result;                    //returns the value where it is called
}




Notice the **int** datatype is in place of the void. That means this function is returning an integer.
When you write a function that doesn't return a value, you put the word void in front. So that's why void is used in front of loop() and setup()/

Thanks much,
What do you mean 'returning' ?....Returning from / going to ?

Try this

void setup() 
{
  Serial.begin(115200);
  int theAnswer = multiplyFunction(4, 5);
  Serial.println(theAnswer);  
  
  Serial.println(multiplyFunction(11, 555));
}

void loop() 
{
}

int multiplyFunction(int x,int y) {    // x & y are declared as integers. 
  int result = x * y; 
  return result;                    //returns the value where it is called
}

The value in the result variable in the function is returned (made available) to the code that called the function. It does not have to be put into a variable but can be used directly as in the second example above.

setup() and loop() don't, in normal use, have a program calling, them so cannot return a value, hence their return type is void.

What do you mean 'returning' ?....Returning from / going to ?

Consider this example:

void setup()         // Function signature
{
   int val;

   val = myFunction();
}

int myFunction()      // Function signature
{
   int analogValue = analogRead(A0);
   return analogValue ;
}

In the sequence of things, in setup() the compiler generates code that defines what val is (an int) and immediately transfer program control to myFunction(). The process of transferring program control from setup() to myFunction() is often referred to as "calling myFunction()". You may also hear the process as "going to", "envoking", and other similar verbs. You will also hear programmers refer to setup() in this process as "the caller"; that is, the caller is the name of the function that is giving up control to the function being called (i.e., myFunction()).

Once control is transferred to myFunction(), the code reads analog pin A0. This also means that myFunction() is "calling" analogRead(). (This process of one function calling another function, calling another function, can go on as many times as you need.) If you look at the documentation for analogRead(), it "returns" or "gives back" an int to whomever called it. In our example, that int value is assigned into analogValue.

The return keyword means that the code in myFunction() wants to give back that int value to the caller. Note: the data type being returned (e.g., analogValue) MUST match the first word on the function's signature. In this example, the function signature is: int myFunction(), so analogValue must be an int or the compiler is going to get a little cranky. Let's assume that analogValue has the numeric value of 300.

Because the code to process the "return analogValue;" statement has been executed, the value 300 has been placed on a "serving platter" called the stack and program control reverts back to the caller. This process of transferring control back to the caller is often referred to as "returning to the caller".

When program control gets back to setup(), the program executes code that slides the int data on the serving platter (i.e., the stack) and assigns it into val.

Whenever you see the word void in a function signature, that means there is no return statement in that function and, therefore, it cannot return a value to the caller. The technical term for the data type mentioned in a function signature is "function data type specifier". So if you look up the signature for random(), you will find its signature is: long random(int seed). Because the function type specifier is a long, this function returns a long data type to its caller. This also means that somewhere in that function is the equivalent statement return val, where val is the pseudo-random number generated by the function. The int seed in the signature says that the called must supply an integer value when it is called.

While long-winded, I hope this is helpful.

econjack:

What do you mean 'returning' ?....Returning from / going to ?

Consider this example:

void setup()         // Function signature

{
   int val;

val = myFunction();
}

int myFunction()      // Function signature
{
   int analogValue = analogRead(A0);
   return analogValue ;
}




In the sequence of things, in *setup()* the compiler generates code that defines what *val* is (an *int*) and immediately transfer program control to *myFunction()*. The process of transferring program control from *setup()* to *myFunction()* is often referred to as "calling *myFunction()*". You may also hear the process as "going to", "envoking", and other similar verbs. You will also hear programmers refer to *setup()* in this process as "the caller"; that is, the caller is the name of the function that is giving up control to the function being called (i.e., *myFunction())*.

Once control is transferred to *myFunction()*, the code reads analog pin *A0*. This also means that *myFunction()* is "calling" *analogRead()*. (This process of one function calling another function, calling another function, can go on as many times as you need.) If you look at the documentation for *analogRead()*, it "returns" or "gives back" an *int* to whomever called it. In our example, that *int* value is assigned into *analogValue*. 

The *return* keyword means that the code in *myFunction()* wants to give back that *int* value to the caller. Note: the data type being returned (e.g., analogValue) [u]MUST[/u] match the first word on the function's signature. In this example, the function signature is: [u]*int myFunction()*[/u], so *analogValue* must be an *int* or the compiler is going to get a little cranky. Let's assume that *analogValue* has the numeric value of 300.

Because the code to process the "*return analogValue;*" statement has been executed, the value 300 has been placed on a "serving platter" called the *stack* and program control reverts back to the caller. This process of transferring control back to the caller is often referred to as "returning to the caller". 

When program control gets back to *setup()*, the program executes code that slides the *int* data on the serving platter (i.e., the *stack*) and assigns it into *val*. 

Whenever you see the word *void* in a function signature, that means there is no return statement in that function and, therefore, it cannot return a value to the caller. The technical term for the data type mentioned in a function signature is "function data type specifier". So if you look up the signature for *random()*, you will find its signature is: *long random(int seed)*. Because the function type specifier is a *long*, this function returns a *long* data type to its caller. This also means that somewhere in that function is the equivalent statement *return val*, where *val* is the pseudo-random number generated by the function. The *int seed* in the signature says that the called must supply an integer value when it is called.

While long-winded, I hope this is helpful.

Thank you for the well laid out explaination, thought it's more than I can digest emmeditatley, will read over a few more times.
I have 5 or six arduino books, but don't recall the mention of 'my function' , to further the confusion for me would be seeing 'int my function' but not seeing an integer in the accompanying parethesis.
I'll come back to this explaination once I have a better grasp of those items.

Thanks much.

A function can have almost any name as long as it does not contain spaces or punctuation.

It is good practice to give functions meaningful names that indicate what they do and the use of 'camelCase' for names is common. So you will see functions such as digitalWrite(), pinMode() etc built into the Arduino environment and you can write your own to do common tasks and/or make program maintenance easier.

The myFunction() used in previous replies was only an example. It could just have well been called wibble() for all the IDE cared !

Whenever you see the word void in a function signature, that means there is no return statement in that function and, therefore, it cannot return a value to the caller.

That's just rubbish.

Any function can have a "return" in it - what is important is if the "return" is followed by an expression or not.
In a non-void function, it is the value of the expression that is returned to the calling function.
In a void function, a "return" simply causes the function to exit straightaway.

@grove: I should have said it cannot have a return statement like:

   return expression1;

where expression1 is some expression that resolves to some form of data. If the return is simply being used as a control element, it can appear within the function body.