473,763 Members | 6,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.E xecute(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.E xecute(input);
}
Jul 22 '05 #1
4 2096

"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.E xecute(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.E xecute(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.E xecute(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********@fas tmail.fm> wrote in message
news:83******** *************** **@posting.goog le.com...
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.E xecute(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
8710
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 The ScreenplayCSS is flawed, for several reasons; -overuse of <div id= tags -doesn't scale screen resolutions (convert from px to in, pt ?) -no media="print" (how much coule be shared between "screen" & "print") -no automatic page breaks (with...
1
1927
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 date. I need to view data for a selected week given the week number or date. need some controls on not allowing the ball number to go >49 or <0 and no duplicates.
22
130394
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 document and closes it with the following code: <body onload="window.print(); window.close();"> This works correctly (or at least the way I expect it to work under MS Internet Explorer, but it cuases Netscape to "crash"
2
2893
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 functionality, but whatever was on the clipboard before is lost. If I disable running JavaScript on the page, the clipboard is unaffected. Viewing the page in Firefox does not have this issue. I think it might be the script below, which makes no sense...
1
1729
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; visibility: hidden; }
4
2866
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
2340
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 Perl Builder which is an Integrated Development Environment for Perl scripting. I am trying to access the data in a txt file and then print the results to the screen. The procedure for doing this is write a script then click on the Run button,...
5
3446
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 shot? If so, how? Thanks,
4
2700
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
2771
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. For example... If I have a style sheet specifying media="all," is the following correct syntax to center text for printing, but not for screen presentation? ..divClass1 {
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.