about "call by value" and "call by reference" 
Author Message
 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.

All parameters to functions are values. Therefore if you want to pass
a "reference" to an object, you must pass the value of its address as
an argument. This is also hte only way to alter the "value" of an
object inside a function - you pass its address and modify whats at
that address.

For example

void foo(int x)

x is an int value. If you change x inside the function, the change is
lost, since x is a local copy of value of the variable.

void foo(int *x)

x is an int pointer value. if you change x you lose the change as
before. However now you can change what x points to, and this change
is remembered.

Mark McIntyre

C- FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html



Fri, 23 Aug 2002 03:00:00 GMT  
 about "call by value" and "call by reference"
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.

Have a Great Day!!!



Sat, 24 Aug 2002 03:00:00 GMT  
 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



Sat, 24 Aug 2002 03:00:00 GMT  
 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.

http://www.eskimo.com/~scs/C-faq/s4.html

Bill, faqqer.



Mon, 02 Sep 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Q: Return-values of system("...") calls

2. "c" calling Fortran, and Fortran calling "c"

3. remove() vrs fopen("""w")

4. Displaying binary data as ascii "1"'s and "0"'s

5. Looking for "Shroud"/"Obfus"

6. ""help with TSR""

7. Parse trees and "("")"

8. Error "free"-ing "malloc"-ed memory

9. Displaying binary data as ascii "1"'s and "0"'s

10. Calling a web service from "C", not C++

11. How to call the function "RasEnumEntries"

12. The mysterious "Call" function

 

 
Powered by phpBB® Forum Software