-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
mi*********@gmail.com wrote:
i am really having a hard time trying to differentiate the
two..........i mean.....anyone got a better idea how each occurs?
My two cents (may be devalued by others in the newsgroup :-) )
With "call by value", the caller passes the /value/ of the source data
to the target. If the target changes the data it receives, it only
changes a copy of the value, not the original data.
With "call by reference", the caller makes the source data directly
available to the target. Both source and target refer to the data in the
same way, and changes that the target makes to the data will be
reflected in the original data.
C uses the "call by value" semantic, not the "call by reference"
semantic. "call by value" using pointers can obtain similar results as
"call by reference", although the semantic is still "call by value" and
the target function must use special operations to make the effects happen.
*IF* C supported "call by reference", the following would be valid
void target(int number)
{
number = 10;
}
void caller(void)
{
int a_number = 3;
target(a_number);
/* at this point, call by reference
** would leave a_number set to 10
*/
}
However, C only supports "call by value", so the above would not result
in a_number (in caller() ) being set to a value of 10 by target().
In C, you can /emulate/ "call by reference" using pointers. The
emulation of the above code would look like:
void target(int *number)
{
*number = 10;
}
void caller(void)
{
int a_number = 3;
target(&a_number);
/* at this point, emulated call by reference
** leaves a_number set to 10
*/
}
Note the difference: in the "call by reference" example above, both
target() and caller() shared a common definition of the data (int). In
the /emulated/ "call by reference", caller() defines the data as int,
and target() defines it's access as pointer. Not true "call by
reference", but does the same thing.
- --
Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFD0Q82agVFX4UWr64RAj1RAKC80K5Bj8miV+lciUUHiM 4waX7k1QCeOs+d
AnCAfnhoyh6UsTU50fh7JnQ=
=gCCs
-----END PGP SIGNATURE-----