Re: Why does this work?

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 03/26/05


Date: Sat, 26 Mar 2005 22:02:42 GMT


"John Peters" <john@jpeters.net> wrote in message
news:d6ydne5YbfWoT9jfRVn-tg@comcast.com...
>
> I'm curious why the cout in the function prints before
> the cout in main.
>
> Thus:
>
> "Calling sum 3 + 4
> The sum is :7"
>
> //Code:
>
> int sum(int x, int y);
>
> #pragma argsused
> int main(int argc, char* argv[])
> {
>
> cout<< "The sum is :" << sum(3,4);
> cin.get();
> return 0;
> }
>
> int sum(int x, int y)
> {
> cout << "Calling sum " <<x <<" + " << y <<"\n";
> return x+y;
> }
>

The following line:

          cout << "The sum is :" << sum(3,4);

sees the 'sum' function called [supplied with the arguments, 3, and 4], and
the code within its body executed, and a result returned - prior to - the
'cout' statement [term used loosely] is, iteself, executed.

In effect, the function call is replaced with it's returned value:

          cout << "The sum is :" << 7;

However, since the body of the function also contains a 'cout' statement,
*it* is executed as part of function execution, thus its output appears
prior to that of the original.

This is an example of what is termed a 'side effect': a function's execution
not only sees a return value computed, but, as a consequence of executing,
impacts on other parts of the program, sometimes in unhelpful ways.

To avoid such possibilities the remedy is to code in a way that avoids, or
at least minimises, such 'side effects'. One way to do this is to design
functions so that they only perform steps that contribute to a single task.

In the current case, the following approach could have been used:

#include <iostream>

int sum(int x, int y);

int main(int argc, char* argv[])
{
        int x = 3, y = 4;
        cout << "Calling sum " << x << " + " << y << endl;
        cout<< "The sum is :" << sum(x, y);
        cin.get();
        return 0;
}

int sum(int x, int y)
{
       return x+y;
}

As you should have noticed, the 'sum' function is now side effect-free, and
can be used in situations not involving console output. This makes it a more
useful function that it was previously.

I hope this helps.

Anthony Borla



Relevant Pages

  • Re: Could use some help...:)
    ... cout << endl ... int B::operator (unsigned row, unsigned column) ...
    (comp.lang.cpp)
  • Re: C++ compile error
    ... hello.C: In function `int main': hello.C:5: error: `cout' undeclared hello.C:5: error: (Each undeclared identifier is reported only once for each function it appears in.) ... cout is part of the 'std' namespace, ... I'm totally puzzled by this complex language when I compile my first program. ...
    (freebsd-questions)
  • Re: Multithread heap assertion failure(Continued)
    ... class CTestThread: public CWinThread{ ... heap (I've seen this happen to people who used cout in this fashion). ... int ExitInstance{ ... This does not set a 1ms timeout; ...
    (microsoft.public.vc.mfc)
  • c++ Linux2Windows text file conversion
    ... int uw(string src,string dest); ... cout << " Error opening source\n"; ... string dest; ...
    (comp.lang.cpp)
  • Re: need help with a While im a beginner
    ... > ascendinf, descending, equal or in no ordre. ... toupper takes a char and you are passing a 'int'. ... How about cin, cout here. ...
    (comp.lang.cpp)