Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > doubt about passing values by address

Reply
Thread Tools

doubt about passing values by address

 
 
johnnash
Guest
Posts: n/a
 
      02-19-2008
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 ?
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      02-19-2008

"johnnash" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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

 
Reply With Quote
 
 
 
 
fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a
 
      02-19-2008
On Feb 19, 7:33*am, johnnash <(E-Mail Removed)> 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
 
Reply With Quote
 
johnnash
Guest
Posts: n/a
 
      02-19-2008
On Feb 19, 8:42 pm, (E-Mail Removed) wrote:
> On Feb 19, 7:33 am, johnnash <(E-Mail Removed)> 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 ?
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      02-19-2008
-----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

iD8DBQFHuvpzagVFX4UWr64RAgaPAKDHZa7xBxUl8ssrEMugjK jHRqhfKgCeLdlZ
WSnIP/R016ls2onTkdCBFz4=
=Olwd
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-19-2008

"johnnash" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 19, 8:42 pm, (E-Mail Removed) wrote:
>> On Feb 19, 7:33 am, johnnash <(E-Mail Removed)> 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




 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      02-19-2008
On Tue, 19 Feb 2008 07:48:28 -0800, johnnash wrote:

> On Feb 19, 8:42 pm, (E-Mail Removed) wrote:
>> On Feb 19, 7:33 am, johnnash <(E-Mail Removed)> 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).



 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      02-19-2008
"Bartc" <(E-Mail Removed)> wrote:
> "johnnash" <(E-Mail Removed)> 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
 
Reply With Quote
 
gw7rib@aol.com
Guest
Posts: n/a
 
      02-20-2008
On 19 Feb, 15:33, johnnash <(E-Mail Removed)> 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.
 
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
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
putting values into array-newbie doubt oswald.harry@gmail.com C Programming 7 01-30-2008 08:11 AM
Doubt passing values Paulo Roberto ASP .Net 3 08-12-2007 01:32 AM
doubt in variable passing in multiple process chaitanyakurmala@gmail.com VHDL 4 10-02-2006 07:32 PM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM



Advertisments