2

In a C program, how does function call by value work, and how does call by reference work, and how do you return a value?

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72

3 Answers3

12

Call by value

void foo(int c){
    c=5; //5 is assigned to a copy of c
}

Call it like this:

int c=4;
foo(c);
//c is still 4 here.

Call by reference: pass a pointer. References exist in c++

void foo(int* c){
    *c=5; //5 is assigned to  c
}

Call it like this:

int c=0;
foo(&c);
//c is 5 here.

Return value

int foo(){
    int c=4;
     return c;//A copy of C is returned
}

Return through arguments

   int foo(int* errcode){

       *errcode = OK;

        return some_calculation
   }
Tom
  • 43,810
  • 29
  • 138
  • 169
5

The C language does not support call-by-reference.

What you can do is pass a pointer (which works as a reference, but is different from what C++ calls a "reference") to the data your function is interested in, which enables you to do most of the things that call-by-reference is good for.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • 1
    The C language standard says that a pointer value *"provides a reference to an entity of the referenced type"* - the C++ term "reference" is a lot more narrow than the wider CS meaning. – caf Nov 02 '09 at 03:56
  • 2
    @caf: You are quite correct, in both your statements. Additionally, "call-by-reference" is a different thing with a meaning that is more similar to C++'s references than the generic "reference" concept of the C language. Call-by-reference introduces specific semantic capabilities, foremost of which is that callees can rebind variables in the caller's context. Passing a pointer is *not* the same, since the callee can mutate the pointed-to object, but cannot rebind the caller's pointer to point to something else. – Daniel Pryden Nov 02 '09 at 04:06
  • @DanielPryden : please could you explain the term rebinding? – sjsam Feb 17 '16 at 12:09
  • 1
    @sjsam: In this case it means to modify the caller's *variable* rather than just the *value* passed. See [this other answer of mine](http://stackoverflow.com/a/34971934/128397) which explains it much more thoroughly. – Daniel Pryden Feb 17 '16 at 23:06
  • @DanielPryden : Thankyou. – sjsam Feb 18 '16 at 02:10
0

Note that if you want to modify a pointer you must pass the pointer itself by reference.

In this example, p is changed on the stack only (in the scope of the function) and will gain its old value when the function exits:

void do_nothing(char *p)
{
    p = (char *)malloc(100);
}

To modify a pointer you must pass it by reference:

void my_string(char **p)
{
    *p = (char *)malloc(100);
}

and the call:

char *str = NULL;
my_string(&str);
...
free(str);
eyalm
  • 3,366
  • 19
  • 21
  • This is a consequence of the fact that passing a pointer is not the same as call-by-reference. Hence, to pass a mutable pointer, you need to pass a pointer (by value) to a pointer that refers to your data. The actual argument is still always passed by value. – Daniel Pryden Nov 03 '09 at 16:31