Thread: void?

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    void?

    i was reading a tutorial and it was about classes. They declare sum things, well ill paste it
    Code:
    class Dog {
    public:
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    private:
        int age;
        int weight;
    };
    well wat does the void before setage and setweight mean?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It means the function is not returning a value.

  3. #3
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    so wat r u sayin, i can set sumthin but i cant output it on da screen?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No what he is saying is that the function does not return a value eg
    Code:
    int foo()
    {
      return 1;
    }
    void bar()
    {
      cout<<"Hello";
    }
    Woop?

  5. #5
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    i know, im a dumbass! but plz explain better

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    OK when you have a function it has to be a type int,float,char,or void or some other value. void does not return a value. You can think of it like this.
    Code:
    #include <iostream>
    
    int add(int a,int b);
    float divide(int a,int b);
    void printStuff(int a,int b);
    
    int main(void)
    {
      int addResults = add(10,5);//This is setting the variable addResults to the return of the function add()
      float divideResults = divide(9,4);//doing the same as the first but with divide
      std::cout<<addResults<<std::endl;
      std::cout<<divideResults<<std::endl;
      printStuff(10,10);//this will do whats in the funtion
      std::cin.get();
      return 0;
    }
    
    int add(int a,int b)
    {
      return (a+b);
    }
    float divide(int a,int b)
    {
      return (a/b);
    }
    void printStuff(int a,int b)
    {
      std::cout<<"a equals: "<<a<<std::endl;
      std::cout<<"b equald: "<<b<<std::endl;
      //notice it doesn't return a value!
    }
    Woop?

  7. #7
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    u guys a really confusing sumtimes

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    What are you confused about? i don't know any other way of explaining it to you if you can't get such a simple concept maybe look into a different thing to pass your time
    Woop?

  9. #9
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    just explain wat u mean by returning a value

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Just as it sounds a function RETURNS A VALUE
    Code:
    int blah()
    {
      return 1;//THIS FUNCTION RETURNS 1!!!!!!!!!!!
    }
    float meh()
    {
      return 4.5//THIS FUNCTION RETURN 4.5!!!!!!!!!!!
    }
    int main(void)
    {
      weee=blah();//THIS EQUALS 1 BECAUSE blah() RETURNS 1!!!!!!!!!!!!!!!
      yeppers=meh();//THIS EQUALS 4.5 BECAUSE meh() RETURNS 4.5!!!!!!!!!!!!
      return 0;//MAIN RETURNS 0!!!!!!!!
    }
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! just for some extra effect
    Woop?

  11. #11
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    thankyou, was that so hard?

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No, but is understanding what return means so hard?
    Woop?

  13. #13
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    it is wen crappy tutorials dont explain , i still dont get it but i aint askin no more, but dont make me come over there biatch(kiddin)

  14. #14
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi polonyman,

    I'm also really new to C++ (and new to the boards here). I found functions a little bit tricky at first too. As a test of how well I understand them let me try and give you my two cents. If I'm wrong, then I'll be glad to hear so from the more experienced folk here.

    I think of functions as kinda like a really big variable, but a variable that does something. You can put stuff in it (arguments) and get stuff out of it (return) if you choose to . You have to tell it, though, what kind of arguments you want it to accept as well as what kind of value to spit back out (return). It spits back the return value at the point where you called the function (usually, I guess, in main, but I suppose it could be called from another function just as easily?).

    The void in front of the function means that you don't want it to return any value.

    You can still output to the screen from a function (for ex. with cout) but this is different than returning a value.

    Does this sound right?

    It actually makes me think just a bit about the return value of main. If functions return their values to main (or some other place within the program) where does main return its value to? My first inclination would be that it returns either to the compiler or to the OS. But I can't be sure of that.

    Anyway, don't know if this helps at all, but hope you can get it sorted out.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    it (one function) could be called from another function just as easily?-----Yes, one function can be call from another and return to calling.

    it (main()) returns either to the compiler or to the OS---main() returns a value to the OS. What the OS does with the return value is up to the OS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM