learning about side-effects

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Jan 15, 2009.

  1. , India

    , India Guest

    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
     
    , India, Jan 15, 2009
    #1
    1. Advertising

  2. , India

    Zeppe Guest

    , 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_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
     
    Zeppe, Jan 15, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Bancroft
    Replies:
    2
    Views:
    2,415
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=
    Dec 28, 2004
  2. Anonieko
    Replies:
    0
    Views:
    1,945
    Anonieko
    Aug 12, 2005
  3. VisionSet
    Replies:
    7
    Views:
    473
    Michael Rauscher
    Nov 9, 2004
  4. Jason Heyes
    Replies:
    10
    Views:
    545
    red floyd
    Dec 21, 2004
  5. Giuseppe

    Re: Newbie-Side effects?

    Giuseppe, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,165
    Giuseppe
    Jun 24, 2003
Loading...

Share This Page