doubt about passing values by address

Discussion in 'C Programming' started by johnnash, Feb 19, 2008.

  1. johnnash

    johnnash Guest

    I saw this particular piece of code in a book which basically adds two
    vectors and produces the sum


    Vector *VectorAdd( Vector *a, Vector *b, Vector *c)

    {

    c->x = a->x + b->x ;

    c->y = a->y + b->y ;

    c->z = a->z + b->z;

    return(c);
    }


    Now my doubt about this code is why did the author decide to return
    the address of c ? Is it not true that when we pass values by
    reference, then there is no need to return anything as changes are
    automatically reflected at the address of the variable ?
     
    johnnash, Feb 19, 2008
    #1
    1. Advertising

  2. "johnnash" <> wrote in message
    news:...
    >I saw this particular piece of code in a book which basically adds two
    > vectors and produces the sum
    >
    >
    > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)
    >
    > {
    >
    > c->x = a->x + b->x ;
    >
    > c->y = a->y + b->y ;
    >
    > c->z = a->z + b->z;
    >
    > return(c);
    > }
    >
    >
    > Now my doubt about this code is why did the author decide to return
    > the address of c ? Is it not true that when we pass values by
    > reference, then there is no need to return anything as changes are
    > automatically reflected at the address of the variable ?
    >

    It's pretty pointless, because caller must already have the address of c.
    However it helps to doucment that c is indeed the return vector.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Feb 19, 2008
    #2
    1. Advertising

  3. johnnash

    Guest

    Re: doubt about passing values by address

    On Feb 19, 7:33 am, johnnash <> wrote:
    > I saw this particular piece of code in a book which basically adds two
    > vectors and produces the sum
    >
    > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)
    >
    > {
    >
    >         c->x  =  a->x + b->x ;
    >
    >         c->y  =  a->y + b->y ;
    >
    >         c->z  =  a->z + b->z;
    >
    >         return(c);
    >
    > }
    >
    > Now my doubt about this code is why did the author decide to return
    > the address of c ? Is it not true that when we pass values by
    > reference, then there is no need to return anything as changes are
    > automatically reflected at the address of the variable ?


    This allows something like this:
    VectorAdd( VectorAdd(&a,&b,&c), &d, &e );

    to perform e=a+b+d
    --
    Fred Kleinschmidt
     
    , Feb 19, 2008
    #3
  4. johnnash

    johnnash Guest

    Re: doubt about passing values by address

    On Feb 19, 8:42 pm, wrote:
    > On Feb 19, 7:33 am, johnnash <> wrote:
    >
    >
    >
    > > I saw this particular piece of code in a book which basically adds two
    > > vectors and produces the sum

    >
    > > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)

    >
    > > {

    >
    > > c->x = a->x + b->x ;

    >
    > > c->y = a->y + b->y ;

    >
    > > c->z = a->z + b->z;

    >
    > > return(c);

    >
    > > }

    >
    > > Now my doubt about this code is why did the author decide to return
    > > the address of c ? Is it not true that when we pass values by
    > > reference, then there is no need to return anything as changes are
    > > automatically reflected at the address of the variable ?

    >
    > This allows something like this:
    > VectorAdd( VectorAdd(&a,&b,&c), &d, &e );
    >
    > to perform e=a+b+d
    > --
    > Fred Kleinschmidt


    but still what is the point in returning address of e ?

    we can directly retrieve the new value of e because the content has
    been changed at the address. Isnt it true ?
     
    johnnash, Feb 19, 2008
    #4
  5. johnnash

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    johnnash wrote:
    > I saw this particular piece of code in a book which basically adds two
    > vectors and produces the sum
    >
    >
    > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)
    >
    > {

    [snip]
    > return(c);
    > }
    >
    >
    > Now my doubt about this code is why did the author decide to return
    > the address of c ?


    The code author's intent is not evident from the code sample, so I can only
    guess at his motivation. When I've written functions that return one of the
    original arguments, I have made that design decision based on the potential
    use of the function; that is to say, could the return value be used as an
    argument to another function? The author of the VectorAdd function may have
    had a similar thought, perhaps intending something like

    void VectorPrint(Vector *a);
    Vector *VectorAdd(Vector *a, Vector *b, Vector *c);
    Vector *VectorDivide(Vector *a, Vector *b, Vector *c);
    Vector vector[3];

    VectorPrint(VectorAdd(vector+0,vector+1,vector+2));
    VectorPrint(VectorDivide(vector+2,vector+0,vector+2));


    > Is it not true that when we pass values by reference,


    In C, we /never/ pass values by reference. We /always/ pass values "by value".
    In this case, the values being passed are pointers to Vectors.

    But, I know what you meant ;-)

    > then there is no need to return anything as changes are
    > automatically reflected at the address of the variable ?


    Poorly worded, and perhaps not what you meant. But sort of, yes.


    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    - ---------- Slackware - Because I know what I'm doing. ------


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)
    Comment: Armoured with GnuPG

    iD8DBQFHuvpzagVFX4UWr64RAgaPAKDHZa7xBxUl8ssrEMugjKjHRqhfKgCeLdlZ
    WSnIP/R016ls2onTkdCBFz4=
    =Olwd
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Feb 19, 2008
    #5
  6. johnnash

    Bartc Guest

    Re: doubt about passing values by address

    "johnnash" <> wrote in message
    news:...
    > On Feb 19, 8:42 pm, wrote:
    >> On Feb 19, 7:33 am, johnnash <> wrote:
    >>
    >>
    >>
    >> > I saw this particular piece of code in a book which basically adds two
    >> > vectors and produces the sum

    >>
    >> > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)
    >> > return(c);


    >> > Now my doubt about this code is why did the author decide to return
    >> > the address of c ? Is it not true that when we pass values by
    >> > reference, then there is no need to return anything as changes are
    >> > automatically reflected at the address of the variable ?

    >>
    >> This allows something like this:
    >> VectorAdd( VectorAdd(&a,&b,&c), &d, &e );
    >>
    >> to perform e=a+b+d


    > but still what is the point in returning address of e ?


    Maybe to further allow:

    VectorAdd(VectorAdd( VectorAdd(&a,&b,&c), &d, &e ), &f, &g);

    etc.

    Returning that address has little cost, which is more than offset by the
    benefit of being able to use the result immediately, in the same expression.
    Especially compared with vector arithmetic.

    True, ultimately the very last VectorAdd would be returning a value for no
    reason. If that worries you, create two versions of VectorAdd(), one
    returning the address and one returning nothing.

    --
    Bart
     
    Bartc, Feb 19, 2008
    #6
  7. Re: doubt about passing values by address

    On Tue, 19 Feb 2008 07:48:28 -0800, johnnash wrote:

    > On Feb 19, 8:42 pm, wrote:
    >> On Feb 19, 7:33 am, johnnash <> wrote:
    >>
    >>
    >>
    >> > I saw this particular piece of code in a book which basically adds two
    >> > vectors and produces the sum

    >>
    >> > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)

    >>
    >> > {

    >>
    >> > c->x = a->x + b->x ;

    >>
    >> > c->y = a->y + b->y ;

    >>
    >> > c->z = a->z + b->z;

    >>
    >> > return(c);

    >>
    >> > }

    >>
    >> > Now my doubt about this code is why did the author decide to return
    >> > the address of c ? Is it not true that when we pass values by
    >> > reference, then there is no need to return anything as changes are
    >> > automatically reflected at the address of the variable ?

    >>
    >> This allows something like this:
    >> VectorAdd( VectorAdd(&a,&b,&c), &d, &e );
    >>
    >> to perform e=a+b+d
    >> --
    >> Fred Kleinschmidt

    >
    > but still what is the point in returning address of e ?
    >
    > we can directly retrieve the new value of e because the content has
    > been changed at the address. Isnt it true ?


    One can write slightly more succinct code this way. Of course
    not everyone thinks brevity is a virtue.

    One example is initialised variables
    given vector* VectorAlloc( void); one could write, e.g.
    Vector* c = VectorAdd( a, b, VectorAlloc());
    Vector* e = VectorAdd( c, d, VectorAlloc());
    ... more declarations whose initialisers involve c,e..

    If one just had void VectorAdd() one would have to write
    Vector* c = VectorAlloc();
    Vector* f = VectorAlloc();
    ... more declarations, but can't use c,e ..
    VectorAdd( a, b, c);
    VectorAdd( c, d, e);
    ... more code from the initialisers ...

    Another example is nested function calls, e.g
    VectorFree( VectorOp( P, VectorAdd( a, b, VectorAlloc())));
    (where VectorOp() returns it's second argument).
     
    Duncan Muirhead, Feb 19, 2008
    #7
  8. Re: doubt about passing values by address

    "Bartc" <> wrote:
    > "johnnash" <> wrote:
    > > > > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)
    > > > > Now my doubt about this code is why did the author
    > > > > decide to return the address of c ?


    That is a good question because it's more common to see
    the design matching a = b + c, returning a.

    > > > > Is it not true that when we pass values by
    > > > > reference, ...


    C only passes parameters and return values by value.
    Pointers are used to emulate passing by reference.

    > > > This allows something like this:
    > > >   VectorAdd( VectorAdd(&a,&b,&c), &d, &e );


    Or perhaps more common...

    SomeFunctionNeedingAVector(VectorAdd(&a, &b, &c));

    > > > to perform e=a+b+d


    More like e = (c = a + b) + d.

    > > but still what is the point in returning address of e ?

    >
    > Maybe to further allow:
    >
    > VectorAdd(VectorAdd( VectorAdd(&a,&b,&c), &d, &e ), &f, &g);
    >
    > etc.


    Fundamental point is that a function can only have one
    signature. [If we forget about tgmath in C99.]

    > True, ultimately the very last VectorAdd would be
    > returning a value for no reason. If that worries you,


    Get over it! ;-)

    > create two versions of VectorAdd(), one returning the
    > address and one returning nothing.


    ...and realise that no C programmer will thank you for
    it. Indeed if asked to implement this, most C programmers
    will shrug and simply do...

    VoidVectorAdd(...)
    {
    /* ignore return value */
    NonVoidVectorAdd(...);
    }

    Who wants to synchronously maintain two functions performing
    the same task?

    --
    Peter
     
    Peter Nilsson, Feb 19, 2008
    #8
  9. johnnash

    Guest

    Re: doubt about passing values by address

    On 19 Feb, 15:33, johnnash <> wrote:
    > I saw this particular piece of code in a book which basically adds two
    > vectors and produces the sum
    >
    > Vector *VectorAdd( Vector *a, Vector *b, Vector *c)
    >
    > {
    >
    >         c->x  =  a->x + b->x ;
    >
    >         c->y  =  a->y + b->y ;
    >
    >         c->z  =  a->z + b->z;
    >
    >         return(c);
    >
    > }
    >
    > Now my doubt about this code is why did the author decide to return
    > the address of c ? Is it not true that when we pass values by
    > reference, then there is no need to return anything as changes are
    > automatically reflected at the address of the variable ?


    As you've spotted, the function doesn't *need* to return the value of
    c. But it does, so that the value is available if you want to use it.
    Other people have given examples where using this return value allows
    things to be written as a single line rather than as two lines.

    It's similar to the situation with strcpy and strcat. Each of these
    two functions returns one of its input parameters as its output. You
    don't need to use this, but sometimes it is handy to.

    Help that helps.
    Paul.
     
    , Feb 20, 2008
    #9
    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. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    629
  2. Paulo Roberto

    Doubt passing values

    Paulo Roberto, Aug 11, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    313
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Aug 12, 2007
  3. Replies:
    0
    Views:
    566
  4. Peter Otten
    Replies:
    2
    Views:
    119
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    120
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page