Thread: What exactly does "void" mean?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    What exactly does "void" mean?

    Well, my question is pretty basic.

    I started programming in C++ back in 2005 for a programming class, but we never covered a lot of it. So a few months ago I started reading articles on this site. I'm trying to compile a game I've been working on, and as good as I am with graphics, my programming skills are not par. I've been learnign C++ in sections, but I don't really understand the whole "void" vs. "int" for functions and things. An example of code would be:

    Code:
    class Animal
    {
      public:
      Animal();
      ~Animal();
      void eat();
      void sleep();
      void drink();
    
    private:
      int legs;
      int arms;
      int age;
    };
    //The class Animal contains information and functions
    //related to all animals (at least, all animals this lesson uses)
    class Cat : public Animal
    {
      public:
      int fur_color;
      void purr();
      void fish();
      void markTerritory();
    };
    //each of the above operations is unique
    //to your friendly furry friends
    //(or enemies, as the case may be)

  2. #2
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I'm new to programming as well, but I thought that it means a function will not return a value. So, void eat(); Would just execute a process that causes the cats "hunger gage" to be refilled but it will not return any value.

    But I might be completely wrong.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In that code, the place you see the word "void" is as a return type for a function. All C++ functions specify the type that they return. If your function returns an integer, you can return int, if it returns a floating-point number, you can return double. If your function does not return anything, you specify "void" as the return type. That way the compiler and the users of the function know that nothing is returned, the function just does some work and finishes.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Hello
    In C++ there are nine types of data types, they are int,char,float,double,void,long,unsigned,signed these are also used as return types for functions (but long,unsigned,signed because they are modifiers)

    when used in return types, the function return the corresponding data

    void is used to indicate that the function returns nothing

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually, there are 14 distinct primitive types, plus void:
    void
    char
    signed char
    unsigned char
    signed short int
    unsigned short int
    signed int
    unsigned int
    signed long int
    unsigned long int
    float
    double
    long double
    boolean
    wchar_t

    There are alternate names: you can drop the "signed" from all types except char. You can drop the "int" from all types, as long as at least one keyword remains.
    Added to these 14 is the non-type, void. A variable of type void cannot exist, because it would contain nothing (and then what would be the point?). A pointer to void is a pointer that points "somewhere", but not specifically to any datatype. A function returning void returns nothing. A function taking void takes no parameters.
    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

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Assuming you're only returning base types, CornedBee is right. However, you can also return classes, etc, allowing for an infinite number of return types.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    [WARNING: CORNY HUMOR AHEAD]

    I think I'll make a class called VOID and return it

    [/WARNING]
    My Website

    "Circular logic is good because it is."

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Yeah ok, then post the compiler error you get
    Double Helix STL

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah ok, then post the compiler error you get
    Why should DavidP get a compile error for a custom VOID class? void and VOID are not the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    i was being sarcastic. I know you do get an error if you return a value from a function that has been delcared void, but I guess I mis-read his post, I thought he was refering to a functon void. Yes a class would be a different matter
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "VOID" as opposed to void
    By Trauts in forum Windows Programming
    Replies: 23
    Last Post: 05-28-2003, 10:56 AM
  2. "void"
    By jjj in forum C Programming
    Replies: 5
    Last Post: 09-08-2002, 12:18 PM
  3. questions about "void"...
    By Gamma in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2002, 10:04 AM
  4. Replies: 1
    Last Post: 11-25-2001, 06:31 AM