
about "call by value" and "call by reference"
Quote:
> I read all funtion arguments in C are passed "by value", not "by
> reference", and
> cannot understand it exactly. I want some more explanation with examples.
Call by value means that the formal parameter -- that is, the variable
actually inside the function -- has the *value* of the actual parameter
*copied* into it. This means that changes to the formal parameter don't
result in changes to the actual parameter.
int foo (void)
{
int actual = 1;
bar(actual);
printf ("%d\n, actual); /* Prints 1 */
}
int bar (int formal)
{
formal = 2;
printf ("%d\n, formal); /* Prints 2 */
}
Call by reference means that the formal parameter becomes a "reference" to
the actual parameter, i.e. just another way of referring to exactly the same
thing. This means that changes to the actual parameter are reflected in the
formal parameter.
If we imagine that C used call by reference, then
int foo (void)
{
int actual = 1;
bar(actual);
printf ("%d\n, actual); /* Would print 2... */
}
int bar (int formal) /* ...because 'formal' is just another */
/* name for 'actual'... */
{
formal = 2; /* ...and we set it to 2 here */
printf ("%d\n, formal);
}
"Call by value" and "call by reference" are also called "pass by value" and
"pass by reference"; it's the same thing.
There is a third method, "pass by name", which is broadly similar to what
happens with macro substitution in C. You're unlikely to encounter it in
any other context.
Quote:
> Have a Great Day!!!
A nice thought, though statistical analysis suggests it is unlikely.
Cheers
Richard
--
The FAQ is at http://www.eskimo.com/~scs/C-faq/top.html
New users try http://www.sin.khk.be/~emulov/c.l.c/welcome_to_clc.htm