Thread: Text in a function printed before it is called

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Text in a function printed before it is called

    Dear All

    I have two C City & Guilds (4240, 4250) and I have decided to continue and learn C++. So I have purchased a book with compiler on CD, Bloodshed Dev-C++. I have written a programme found in the book and the output is as the book describes. However, I do not understand it. I read on to see if the book explined my question but it did not. See if you can.




    Code:
    #include <iostream>
    
    int Add (int x, int y)
    {
        std:: cout << "In Add(), received " << x << " and " << y  << "\n";
        return (x+y);
    }
    
    int main()
    {
         std:: cout << "The value returned is : " << Add(3,4);
         return 0;
    }
    Code:
    Output to screen
     In Add(), received 3 and 4
     The value returned is : 7
    And the problem:
    Why does the text in the function get printed to screen before
    the function is called?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    // Because this function returns
    
    Add(3,4);
    
    //before this function returns
    
    cout << Add(3,4);
    This actually led me to a question myself, though. What's the deal with
    Code:
    std::cout.operator<<("The value returned is : ");
    returning an address?
    Last edited by SlyMaelstrom; 06-14-2006 at 04:13 PM.
    Sent from my iPadŽ

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    the << operator is right associative. Which means it reads from its right operand and assigns the result to its left operand. As so we must evaluate the right-most operand first...

    std:: cout << "The value returned is : " << Add(3,4);
    Add() is evaluated first. The cout inside gets then printed to screen because a function termination flushes any local stream. The result meanwhile is sent to the stream in main().

    std:: cout << "The value returned is : " << 7;
    main() then finishes and the stream is flushed, producing the 2nd line of the output
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your cout call in main is equivalent to:
    Code:
    operator<<(operator<<(std::cout, "The value returned is : "), Add(3,4));
    The first operator<< function call has two arguments, both of which are the return values of function calls themselves (the first is operator<<(std::cout, "The value returned is : "), the second is Add(3,4)). The order that the two arguments are evaluated isn't specified by the standard, so you don't know which of those two functions will be evaluated first. On your machine with your compiler, the second function was evaluated first, which is why the stuff inside of it got printed first.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> What's the deal with std::cout.operator<<("The value returned is : "); returning an address?

    It returns the stream, not an address, but the stream can be converted to void* so that it can be used in a boolean expression.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Daved
    The order that the two arguments are evaluated isn't specified by the standard, so you don't know which of those two functions will be evaluated first. On your machine with your compiler, the second function was evaluated first, which is why the stuff inside of it got printed first.
    In fact the output operator has an associative rule. It is right associative. The right operands are evaluated first.
    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.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Daved
    >> What's the deal with std::cout.operator<<("The value returned is : "); returning an address?

    It returns the stream, not an address, but the stream can be converted to void* so that it can be used in a boolean expression.
    That makes sense, I wasn't thinking straight. Thank you.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It is right associative.
    I don't think that's correct, but I don't have any references handy to prove it. The bitshift operators are left associative, and so is the overloaded version used with streams. All that the associativity means is that you will get:
    Code:
    operator<<(operator<<(std::cout, "The value returned is : "), Add(3,4));
    and not:
    Code:
    operator<<(operator<<(std::cout, Add(3,4)), "The value returned is : ");
    The order of evaluation of the parameters should still be unspecified.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Thanks to all who replied

    A very useful lesson

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Daved
    I don't think that's correct, but I don't have any references handy to prove it.
    Actually you are right. I'm sorry. They are left associative. Couldn't be any other way in fact.
    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. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM