VOID (my understanding is void)

This is a discussion on VOID (my understanding is void) within the C++ Programming forums, part of the General Programming Boards category; Could somebody explain VOID to me .... why is it needed, what does it do, are there other options, etc ...

  1. #1
    Registered User
    Join Date
    Sep 2006

    VOID (my understanding is void)

    Could somebody explain VOID to me ....

    why is it needed, what does it do, are there other options, etc

    void SimpleFunc (void)
      cout << "We are in SimpleFunc" << endl;

  2. #2
    Registered User Lawn Gnomusrex's Avatar
    Join Date
    Oct 2008
    Leading lawn gnomes on the path to world domination! ;o)


    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.

    void swap_array_elements(int *pointer1, int *pointer2) {
        int temporary_variable = *pointer2;
        *pointer1 = *pointer2;
        *pointer2 = temp;
    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.

    If you tried to print a message with int, double, or char, it would have to return a value like this:
    int print_hello() {
        cout << "Hello, world!" << endl;
        return 1;
    You would be forced to have "return <insert value here>", but with void, you are not forced to return a value.

    One other important thing to note is that a void function is not allowed to return a value. example:
    void addition() {
        int i = 1 + 1;
        return i; //ERROR! return statement with a value, in function returning void!
    You could however do something along the lines of this.
    void addition(int *n) {
        *n = 1 + 1;
    One last thing: You can use the return statement to exit the function early, it just cannot have a value.
    void print_hello() {
        cout << "Hello, world!" << endl;
        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 06:41 AM. Reason: Thought of additional details that might be of use.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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.

  4. #4
    The larch
    Join Date
    May 2006
    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:
    #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("&#37;s %d\n", str, n);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Dont forget void* pointers, which has absolutely nothing to do with void

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Quote Originally Posted by Magos View Post
    Dont forget void* pointers, which has absolutely nothing to do with void
    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
    struct point {
      unsigned int x;
      unsigned int y;
    has the possible values
    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

    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:
    enum void {}
    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.

    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:
    unsigned int foo();
    This function says, "I'll return one of unsigned int's possible values, i.e. 0, 1, 2, ... UINT_MAX."

    void bar();
    This function says, "I'll return one of void's possible values, i.e. none." In other words, the function returns no value.

    See how wonderfully consistent this is?

    The next important use is in void pointers. Let's again examine the normal pointer first.
    unsigned int* iptr;
    iptr here is a variable of type "pointer to unsigned int".
    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.

    void* vptr;
    This is a "pointer to void".
    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 03:13 PM.
    All the buzzt!

    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  5. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21