Why does it print to screen?

A

Anon Email

Hi people,

Another question. In the code below, why is there a screen output?
This part of the code:

bool status = TheCalculator.Execute(input);

is merely assigning the result of the function call to status. In
fact, I don't understand why I am allowed to perform this assignment
at all, since the Execute method contains more than just a boolean
return. Any advice appreciated.

Cheers,

Deets

Code:
-------------

#include <iostream>
using std::cout;
using std::endl;

class Input {};

class Calculator
{
public:
bool Execute (Input & input)
{
cout << "Why does this text output to screen?\n";
return true;
}
};

int main()
{
Calculator TheCalculator;
Input input;
bool status = TheCalculator.Execute(input);
}
 
B

Buster

Hi people,

Another question. In the code below, why is there a screen output?
This part of the code:

bool status = TheCalculator.Execute(input);

is merely assigning the result of the function call to status. In
fact, I don't understand why I am allowed to perform this assignment
at all, since the Execute method contains more than just a boolean
return. Any advice appreciated.

Cheers,

The function you call is not pure. It has side effects. Think of it
as a procedure or subroutine, which also happens to return a
value. 'Function' is the C++ name for any such procedure.
Even "void f () { std::cout << "Hello!\n"; }" counts as a
'function'.

Regards,
Buster
 
K

Kevin Goodsell

Anon said:
Hi people,

Another question. In the code below, why is there a screen output?

Because there is an output statement in the code, and that statement is
executed. Why does this surprise you? What did you expect to happen?
This part of the code:

bool status = TheCalculator.Execute(input);

is merely assigning the result of the function call to status.

Yes, after executing the function. And executing the function means
executing the output statement.
In
fact, I don't understand why I am allowed to perform this assignment
at all, since the Execute method contains more than just a boolean
return. Any advice appreciated.

I don't understand what you are saying. Why would you not be allowed to
assign the bool return value of a function to a bool object?
Code:
-------------

#include <iostream>
using std::cout;
using std::endl;

class Input {};

class Calculator
{
public:
bool Execute (Input & input)
{
cout << "Why does this text output to screen?\n";
return true;
}
};

int main()
{
Calculator TheCalculator;
Input input;
bool status = TheCalculator.Execute(input);
}

-Kevin
 
M

Mike Wahler

Anon Email said:
Hi people,

Another question. In the code below, why is there a screen output?

Because the code is written to do so.
This part of the code:

bool status = TheCalculator.Execute(input);

is merely assigning the result of the function call to status.

That's not the only thing it does. It first executes the body
of the 'Execute()' function, which contains a statement which
writes to 'cout'.

In
fact, I don't understand why I am allowed to perform this assignment
at all,

Because the type of 'status' and that of the function's return
value are compatible (in this case, they're exactly the same type
-- 'bool'). Anything else (if anything) that happens in the
function doesn't change this fact.
since the Execute method contains more than just a boolean
return.

It's only the return value from a function which can be assigned
by its caller. The 'Execute' function does indeed do more
than return a value. First it writes to 'cout', then it returns
a value of 'true'.
Any advice appreciated.

Um, try researching the term 'side effect'. Expressions (which
include function calls) might or might not have side effects.
Your function 'Execute' has a side effect -- output to 'cout'.
Cheers,

Deets

Code:
-------------

#include <iostream>
using std::cout;
using std::endl;

class Input {};

class Calculator
{
public:
bool Execute (Input & input)
{
cout << "Why does this text output to screen?\n";


Because that's what 'cout <<' means. That's what this function does,
and you've invoked it above.
return true;
}
};


#include <iostream>

bool f2();

bool f1()
{
std::cout << "Hello";
return f2(); /* call f2() and return what it returned */
}

bool f2()
{
std::cout << " world!\n";
return false;
}

int main()
{
f1(); /* note that the return value need not be saved at all,
(unless needed by your logic of course) */
return 0;
}

Output:

Hello world!

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top