Printing a return value

This is a discussion on Printing a return value within the C++ Programming forums, part of the General Programming Boards category; How can one print the return value of a function? For example, you have an 'Add' function which returns the ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Thumbs up Printing a return value

    How can one print the return value of a function? For example, you have an 'Add' function which returns the sum of two argument integers. However, when you return a value, and run the function, the value is not printed. So what is the best way to print such a value?

    I know I could do this, but I don't want to:
    Code:
    int Add(int num1, int num2)
    {
         int sum;
         
         sum=num1+num2;
         
         cout << sum;
    }
    I tried 'couting' a function with a return value, and this works, but is this legal?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    EDIT - Whoops. I completely misinterpreted what you meant. Sorry.

    Do what Mario said. Alternatively -

    int Result = Add( 4, 2 );
    std::cout<< Result;
    But there's no reason why you should need the int variable
    Last edited by twomers; 10-01-2006 at 09:34 AM. Reason: Code Coloring :)

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Code:
    std::cout << Add(12, 23);
    and you can remove the cout from inside the function
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Code:
    int Add(int num1, int num2)
    {
         int sum;
         
         sum=num1+num2;
         
         cout << sum;
    }
    This code is wrong because this function should return an int.

    Code:
    int Add(int num1, int num2)
    {     
        return(num1+num2);
         
    }
    This one returns the sum of its arguments so you can print them if you want.

    Code:
    cout << Add(3, 9);
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Oh ok, so it is fine to 'cout' a function?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> Oh ok, so it is fine to 'cout' a function?

    As long as the function return something cout-able.

    If I had a class like so

    Code:
    class thing
    {
    private:
    	int MyInt;
    	std::string MyString;
    
    public
    	thing();
    };
    And tried to cout an instance, it won't work. You'll have to overload a << function to define how it acts in that respect. Same with (i)(o)fstreams

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    as twomers said with the addition that you will not be couting a function. The function will be evaluated and it is the return value that is used by the cout object
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 03:04 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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