-----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-----