Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > learning about side-effects

Reply
Thread Tools

learning about side-effects

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      01-15-2009
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
 
Reply With Quote
 
 
 
 
Zeppe
Guest
Posts: n/a
 
      01-15-2009
http://www.velocityreviews.com/forums/(E-Mail Removed), India wrote:
> 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_ef...mputer_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: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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[I'm learning C]: Learning to use ucontext Andrey Popp C Programming 5 01-31-2012 01:05 AM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
e-learning, (collaborative learning environment) collinm Java 1 09-08-2005 09:52 PM
Learning about SMS technology... VM Wireless Networking 2 07-02-2004 02:36 PM
learning VHDL Vilvox VHDL 5 07-20-2003 08:59 AM



Advertisments