Could somebody explain VOID to me ....
why is it needed, what does it do, are there other options, etc
ThanksCode:void SimpleFunc (void) { cout << "We are in SimpleFunc" << endl; }
Could somebody explain VOID to me ....
why is it needed, what does it do, are there other options, etc
ThanksCode:void SimpleFunc (void) { cout << "We are in SimpleFunc" << endl; }
Let's see if I can to answer your question.
Void means that the function does not have to return a value. Sometimes it wouldn't be useful or meaningful to return a value like int, double, char, or any other valid type. Usually you use void if you do not need a return value, so it's useful when working with pointers.
For example, let's say you want to make a program that sorts array elements. You might do something similar to this.
As you can see, it doesn't require you to have a return statement. So basically, you use void when you decide that you don't have a use for a function to return a value.Code:void swap_array_elements(int *pointer1, int *pointer2) { int temporary_variable = *pointer2; *pointer1 = *pointer2; *pointer2 = temp; }
If you tried to print a message with int, double, or char, it would have to return a value like this:
You would be forced to have "return <insert value here>", but with void, you are not forced to return a value.Code:int print_hello() { cout << "Hello, world!" << endl; return 1; }
One other important thing to note is that a void function is not allowed to return a value. example:
You could however do something along the lines of this.Code:void addition() { int i = 1 + 1; return i; //ERROR! return statement with a value, in function returning void! }
One last thing: You can use the return statement to exit the function early, it just cannot have a value.Code:void addition(int *n) { *n = 1 + 1; }
Code:void print_hello() { cout << "Hello, world!" << endl; return; cout << "Too bad this line will never execute, *sigh*" << endl; }
I hope that I've answered your question!
(I apologize if I've misunderstood your question, or did a poor job of answering it.)
Last edited by Lawn Gnomusrex; 11-07-2008 at 07:41 AM. Reason: Thought of additional details that might be of use.
And to add one little bit, the void inside the parameter list as void function(void) means that the function does not actually use any parameters.
This is also one of the differences between C and C++. In C++, void foo(); and void foo(void); mean exactly the same (function takes no parameters), in C the former means that the prototype leaves the number and types of arguments unspecified - the compiler will accept anything where the function is called.
Compare the following compiled as C and C++ code:
Code:#include <stdio.h> void foo(); //compiles with this in C, but not in C++ //void foo(void); //does not compile with this in C nor C++ int main (void) { foo(1, "Hello"); return 0; } void foo(int n, const char* str) { printf("%s %d\n", str, n); }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Dont forget void* pointers, which has absolutely nothing to do with void
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Not true, if you use a proper definition of void.
Void is the non-type. There cannot be an object of type void, because void, if you treat is as a type, has no possible values.
Think of it this way: since memory is limited, it is theoretically possible enumerate every single value a variable could possibly have. So, for example, an unsigned int can have the values 0, 1, 2, and so on up to UINT_MAX (for 32-bit integers, that's 2^32 - 1). A signed int can have INT_MIN ... -1, 0, 1 ... INT_MAX. Even a float is enumerable, although knowing just which values it can enumerate means knowing how floating point numbers work, and how exactly the float representation works.
A struct can be enumerated. The struct
has the possible valuesCode:struct point { unsigned int x; unsigned int y; };
0/0, 0/1, 0/2, ... 0/UINT_MAX
1/0, 1/1, 1/2, ... 1/UINT_MAX
2/0, 2/1, 2/2, ... 2/UINT_MAX
.................................
UINT_MAX/0, UINT_MAX/1, UINT_MAX/2, ... UINT_MAX/UINT_MAX
Same for arrays, and thus strings. Enums obviously can be enumerated, since they are defined by enumerating their possible values.
We could go even further. We could say that a type is defined by the enumeration of its values.
(This is not formally true. A type is defined by its canonical name. There are many instances in C++ where two differently named types have the same set of values. However, it may help understanding what's to come.)
Now, every normal type in C++ has at least one possible value. Void doesn't. If empty enums were allowed, you could get something very similar to void by doing:
There is no way to give an object of type void a value, since there aren't any values of type void. Void can be defined as the type that has no values.Code:enum void {}
With me so far?
Now, let's get into the practical uses of void. The most common use is for a function that doesn't return a value. Let's first look at a function that does return a value:
This function says, "I'll return one of unsigned int's possible values, i.e. 0, 1, 2, ... UINT_MAX."Code:unsigned int foo();
This function says, "I'll return one of void's possible values, i.e. none." In other words, the function returns no value.Code:void bar();
See how wonderfully consistent this is?
The next important use is in void pointers. Let's again examine the normal pointer first.
iptr here is a variable of type "pointer to unsigned int".Code:unsigned int* iptr;
The "pointer to" part means that it contains a memory address.
The "unsigned int" part means that at this memory address, one of unsigned int's possible values is stored, i.e. 0, 1, 2, ... UINT_MAX.
So this is a pointer you can dereference, so that you can then read the value there.
This is a "pointer to void".Code:void* vptr;
The "pointer to" part again means that it contains a memory address.
The "void" part means that at this address, there's one of void's possible values, i.e. none. This is a bit trickier than the functions above. The pointer is saying that there isn't a value where it points to. On the other hand, as long as it's not a null pointer, there is some memory there, which has a bit pattern. You can't dereference the void pointer, since you wouldn't get a value. However, you can cast the pointer and thus give an interpretation to the bit pattern.
Finally, there's the argument list void. This one is a bit of a hack. Formally, it says, "I take no value as the first argument". This is then reinterpreted as, "I take no values as arguments at all", probably since it would be stupid to take nothing as the first argument but something as a subsequent argument.
As you can see, it's not something totally different at all. Void fits beautifully into the general type system of C and C++.
Last edited by CornedBee; 11-07-2008 at 04:13 PM.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law