user16062226
user16062226

Reputation:

What would std::cout before calling my function do in this case?

#include <iostream>
    
// **Change needs_it_support so that it returns support:**
bool needs_it_support() {
      
    bool support;
      
    std::cout << "Hello. IT. Have you tried turning it off and on again? Enter 1 for yes, 0 for no.\n";
    std::cin >> support;
    return support;
}
    
int main() {
      
    // **Change the following line to print the function result:**
    needs_it_support();  
      
}

I understand that to make this work the way I want to, I would have to do:

std::cout << (call my function)

But, my question is, what does that do? Wouldn't that just execute all my code and try to output it to the console, and then obviously give me errors? Or, does it only output specific things?

Upvotes: 0

Views: 100

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596713

std::cout << needs_it_support()

This will call needs_it_support() first, and then pass its bool return value to operator<<. It is essentially calling:

std::cout.operator<<(needs_it_support())

So, whatever bool value needs_it_support() returns, that is what will be printed to the console (in addition to anything else that needs_it_support() itself prints internally).

Upvotes: 1

Related Questions