473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Why does it print to screen?

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);
}
Jul 22 '05 #1
4 2080

"Anon Email" wrote
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
Jul 22 '05 #2
Anon Email wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #3
"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
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

Jul 22 '05 #4
Makes sense now.

Thanks for your great answers.

Cheers,

Deets.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: David J Patrick | last post by:
I'm trying to rewrite the CSS used in http://s92415866.onlinehome.us/files/ScreenplayCSSv2.html. using the w3.org paged media standards as described at http://www.w3.org/TR/REC-CSS2/page.html ...
1
by: carl bloc | last post by:
Have this much done but need help with this bit of code: allow user to modify existing draw data. I think I need a counter to give week numbers so the user can select a week number rather than a...
22
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
2
by: Jim Carr | last post by:
Upon entering the site www.FutureByDesign-Music.com with IE6, my clipboard is erased and then disabled in all other Windows XP applications. Navigating to another site returns clipboard...
1
by: tdartt | last post by:
Howdy! I'm fairly new to using css. I have two style sheets: <LINK href="printstyle.css" type="text/css" rel="stylesheet" media="print"> Which has (for example): .dgAltLine { display: none;...
4
by: william.oram | last post by:
alt.html pointed me here, so... I have two CSS files for print and screen. In screen.css: ..screen { font-weight:bold; font-size:12px; }
3
by: Max58kl | last post by:
Trying to access data and print it to the screen using Perl Builders I/O Window -------------------------------------------------------------------------------- Hi I am using a program called...
5
by: bdy120602 | last post by:
Is it possible, when a user or viewer of your Web page, prints or takes a screen shot of a Web page with mousover (roll-over) text in it, to have that text printed or captures as part of the screen...
4
by: Robson Felix | last post by:
It may sound like a silly question, but I would like to print a form or component from within Visual Studio when designing such form component. Is that possible?
10
by: Ed Jay | last post by:
I do not want to load two style sheets for screen and print media. I'm having difficulty grasping the use of the @print statement, or its syntax. Would someone please provide a simple explanation....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.