brand new to arduino

bought arduino hasnt arrived yet but am studying code

i dont understand the void instruction, ive read that the void instruction will not return a value to the higher program, is this like if i wanted to use the value at a certain pin for something else ie

if pin 2=high then LED on

void would not return the value at pin 2 so the led would never light up it is hard for me to understand please help with an example as to when i would use it or when not too i am only 15 and everything i have learned was either from my dad or learned online from reading how to sites, this is all new so please be kind i am very new :)

When you declare functions, you have to tell the compiler about them. You tell the compiler what type of variable you intend to pass back to callers, and what type of variable to expect from callers. 'void' says "I expect NONE of that.

e.g.

int hello(void) { return 1; }

says, "hello is a function taking no parameters, and returning an int."

void setup(void) {} says, "setup is a function taking no parameters and returning nothing.

@RElapse: try reading the first chapters of a C or C++ tutorial on the web. It will give you an idea how programming works. The C++ tutorial at http://www.learncpp.com/ is a good example.

Wouldn’t it be better if the compiler could be made to figure this sort of stuff out by itself?

Wouldn’t it be better if the compiler could be made to figure this sort of stuff out by itself?

How would the compiler know whether it was what you wanted or a mistake that you made? If it guesses wrong you get incorrect results.

The reason you put ‘void’ isn’t to help the compiler, it’s to help whoever is reading/using your code. Often, functions will be declared before they are defined. This lets the programmer and other functions know the required syntax for a function call without needing the full definition.

Let’s say I have the following method declared (without void):

OrderAPizza(char *pizza, int numToppings);

If someone comes along and wants to use the function, they will have to search for the definition to figure out what it returns, if anything. Maybe it returns the cost of the pizza, or an order number, or an error code. Even if the compiler could figure it out, just adding a ‘void’ to the front lets the programmer know they don’t need to check anything.

As far as putting ‘void’ in the arguments list to indicate no arguments, that is a matter of taste. I think empty parenthesis are enough to indicate no arguments.

in a similar but unrelated issue:
You will find that some people will not declare parameter names. The compiler allows this as long as the definition contains the names. This is often frustrating to someone trying to understand the code.

For Instance, using the above declaration:

void OrderAPizza(char *, int);

The compiler is more than happy with this declaration and will proceed without problems. But if you don’t leave any comments telling the coder what the first and second arguments are for, you are the devil. Self-explanatory code is way more efficient and effective than needing comments for everything.

kidmosey:
The reason you put ‘void’ isn’t to help the compiler, it’s to help whoever is reading/using your code.

It’s both. If you declare a function as “void” and try to return a value, the compiler will catch the error.

Let’s say I have the following method declared (without void):

OrderAPizza(char *pizza, int numToppings);

That’s not valid ISO C++, according to my compiler - it requires a type declaration for the function.

oscarBravo:

kidmosey: The reason you put 'void' isn't to help the compiler, it's to help whoever is reading/using your code.

It's both. If you declare a function as "void" and try to return a value, the compiler will catch the error.

Let's say I have the following method declared (without void):

OrderAPizza(char *pizza, int numToppings);

That's not valid ISO C++, according to my compiler - it requires a type declaration for the function.

Those were hypothetical "if the compiler could guess the type" situations. I should have specified