Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    c++ void returning vs other kinds

    Just wondering:

    if you set up a void function to do something like:

    void blah(int x);
    { return x; }

    Why can't you use it in a situation like:

    cout << "this is x" << blah(input) << endl;

    I know if "blah" is an int function, int blah(whatever), it'll work, but the void is returning the same thing; why does this work/not work the way it does? Thanks

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    The keyword void/int before function name is specifying the type of the value to be returned. A function with void type is supposed to return nothing!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, more than I knew before, thanks. But then, if I were to put a cout statement in there, like:

    void blah()
    { cout << "hello" << endl; }

    it'd return a statement to the screen "hello" just fine, so why when it's returning just a number why you can't simply use it as a number. Is it considered something like a string in that case?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    void blah()
    { cout << "hello" << endl; }
    It's not returning or trying to return anything. It just echoes a "hello", when called.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    More specifically, in that case blah() writes "hello" to the cout stream; but that isn't returning anything.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •