Thread: return something in a void

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    return something in a void

    well my question is:

    if i want to return something from a void how do i do that like:
    Code:
    void calculater()
    {
    cout << "5+5=";
    return 10;
    }
    int main()
    {
    int number=calculater();
    cout <<number;
    getchar();
    }
    how?

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you don't. you change the return type

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    how cna you give me a clue?

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    int calculator()
    {
    //...
    }

  5. #5
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    You can not do that.

    void specifically means no return

    if you wan't to return an integer, just declare your function to return an integer:
    Code:
    int MyFunction()
    {
        cout << "My Statement";
        return 10;
    }
    Also please indent code when you post it.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    oh thx you, can you return a string to?`;D

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can return a pointer to a char, or you could accept a pointer as an argument and manipulate that string.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vrkiller View Post
    oh thx you, can you return a string to?`;D
    Yes:
    Code:
    std::string MyFunction()
    {
        return "Some String";
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    ok well then how do i recieve my things i have returned?

    man i maybe like this forum

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vrkiller View Post
    ok well then how do i recieve my things i have returned?

    man i maybe like this forum
    x = function().

    As long as x is of the same type as what function returns, or it is something that can be automatically converted (e.g. int to float, float to int, char * to string) it shouldn't cause any problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    thx ;D. Thread complete and closed

  12. #12
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you should really do the tutorials.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 04:17 PM
  5. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM

Tags for this Thread