learning about side-effects

S

subramanian100in

Kindly bear with me for this long post.

I have the following understanding about side-effects of a function.
Please correct me wherever I am wrong.

Suppose 'Test' is a user-defined type and 'testObj' is an instance of
'Test' class. If 'testObj' is a global object or a namespace-scope
object and if fn() is a function which modifies 'testObj', then fn()
is said to have side-effects. Also if fn() has 'testObj' as a local
static object and if fn() modifies this local static object, then also
fn() is said to have side-effects. If 'Test' class has a static data
member and if fn() modifies this static data member of 'Test' class,
then can fn() be said to have side-effects ?

Suppose
int var;
is a global variable or a namespace-scope variable or a local static
variable. Then if the function fn() modifies the variable 'var', then
can fn() be said to have side-effects ?

If a function modifies only automatic variables or automatic user-
defined-type objects, then the function does not have side-effects. Am
I correct ?

Can someone please explain the definition of side-effects, in simple
terms so that I can understand ?

If a function writes something to 'cout' object, why is it said that
the function has side-effects - what I am trying to ask is, in what
way 'cout' object is modified in order for the function to be said to
have side-efects ?

Thanks
V.Subramanian
 
Z

Zeppe

Kindly bear with me for this long post.

I have the following understanding about side-effects of a function.
Please correct me wherever I am wrong.

I'll try. Check out also
http://en.wikipedia.org/wiki/Side_effect_(computer_science)
Suppose 'Test' is a user-defined type and 'testObj' is an instance of
'Test' class. If 'testObj' is a global object or a namespace-scope
object and if fn() is a function which modifies 'testObj', then fn()
is said to have side-effects. Also if fn() has 'testObj' as a local
static object and if fn() modifies this local static object, then also
fn() is said to have side-effects. If 'Test' class has a static data
member and if fn() modifies this static data member of 'Test' class,
then can fn() be said to have side-effects ?

yes. In general, side-effect is whatever is permanently modified by a
function call.
Suppose
int var;
is a global variable or a namespace-scope variable or a local static
variable. Then if the function fn() modifies the variable 'var', then
can fn() be said to have side-effects ?
yes.


If a function modifies only automatic variables or automatic user-
defined-type objects, then the function does not have side-effects. Am
I correct ?

the point is, take a snapshot of the program state before the function
call, and take another snapshot after the call. Whatever is changed, is
the side effect. So well, for example if a function modifies an argument
passed by reference is a side effect, but if the only thing it does is
to modifies variables local to that function, and a value is returned,
there are no side effects.
Can someone please explain the definition of side-effects, in simple
terms so that I can understand ?

see above.
If a function writes something to 'cout' object, why is it said that
the function has side-effects - what I am trying to ask is, in what
way 'cout' object is modified in order for the function to be said to
have side-efects ?

well, if you consider the function operator<<(std::eek:stream& out,
/*whatever*/), this function will modify the first passed argument. The
details of the modifications are not relevant (in this case, what can
happen is that new data is appended to the output buffer and maybe this
one is flushed to the physical output). The important thing is, the
state of the program is not the same before and after the function call.

Differently, think about a function
int max(int a, int b) { return a > b ? a : b; }

if I call this function, the state of the program won't change. The only
way for me to make use of the function is to "grab" the output.

Best wisehs,

Zeppe
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top