7

I heard that in "c" that there are we can pass the arguments via "call by value" or "call by reference". But in one book it's mentioned that there are we can pass the arguments via both way but there is no "pass by reference" but I actually pass the mostly arguments by "pass by reference".

So why it is mentioned "does C even have "pass by reference"?

A detailed description will be greatly appreciated.

Varun Chhangani
  • 1,116
  • 3
  • 13
  • 18
  • 3
    There is no pass-by-reference in C. – Oliver Charlesworth Jun 18 '13 at 12:18
  • where did you `hear` it? and how do you manage to `pass-by-reference` in C? (You are be mistaken) – Suvarna Pattayil Jun 18 '13 at 12:18
  • @SuvP there is no thing pass by reference in "c" but pass by reference means that we are passing the address to access the data via pointer but my question is that while we are passing the address to access the data why it's mentioned there is no "pass by reference". while we are accessing that same data via call by value or call by reference. – Varun Chhangani Jun 18 '13 at 12:24

2 Answers2

24

C parameters are always passed by value rather than by reference. However, if you think of the address of an object as being a reference to that object then you can pass that reference by value. For example:

void foo(int *x)
{
    *x = 666;
}

You ask in a comment:

So why do we need pointers in C when we can pass all the parameters by value?

Because in a language that only supports pass-by-value, lack of pointers would be limiting. It would mean that you could not write a function like this:

void swap(int *a, int *b)
{
    int temp = *a;
    *b = *a;
    *a = temp;
}

In Java for example, it is not possible to write that function because it only has pass-by-value and has no pointers.

In C++ you would write the function using references like this:

void swap(int &a, int &b)
{
    int temp = a;
    b = a;
    a = temp;
}

And similarly in C#:

void swap(ref int a, ref int b)
{
    int temp = a;
    b = a;
    a = temp;
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @ David Heffernan there is no thing pass by reference in "c" but pass by reference means that we are passing the address to access the data via pointer but my question is that while we are passing the address to access the data why it's mentioned there is no "pass by reference". while we are accessing that same data via call by value or call by reference. – Varun Chhangani Jun 18 '13 at 12:26
  • @David Heffernan I know that so why we need pointer in "c" when we can pass all the parameters by value???? – Varun Chhangani Jun 18 '13 at 12:28
  • 3
    If you did not have pointers, then you could not "fake" pass by reference. Or as Kerrek puts it (more clearly than I did), without pointers you would not be able to assemble reference semantics yourself. – David Heffernan Jun 18 '13 at 12:30
  • @ David Heffernan This is the actual answer which I want Thanks – Varun Chhangani Jun 18 '13 at 12:42
  • You say that Java 'only has pass-by-value' - primitive types are passed by value, but all objects and arrays are passed by reference. – Bertie Wheen Jun 18 '13 at 13:10
  • 1
    @BertieWheen No they are not. Those types are already references. This is a very common mis-understanding. When you pass an object as a parameter you are passing the reference by value. You cannot write a swap function for objects either in Java. Read this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – David Heffernan Jun 18 '13 at 13:12
  • I see what you're saying. However, in that case the 'and has no pointers' part isn't correct, if you're thinking of every object as actually a pointer to an object. – Bertie Wheen Jun 18 '13 at 13:16
  • 1
    @BertieWheen The way I think of it is that the Java implementation uses pointers, but that the Java language has no pointers. Anway, you cannot implement `swap` in Java can you? – David Heffernan Jun 18 '13 at 13:23
  • Not with the given method signature, no. – Bertie Wheen Jun 18 '13 at 13:25
  • @BertieWheen Not with any signature! – David Heffernan Jun 18 '13 at 13:29
  • @BertieWheen No, I'm not counting that. What use is that? – David Heffernan Jun 18 '13 at 13:50
  • @DavidHeffernan When I first learn C++ in class I was taught that with the & in argument of primitive types it is "call-by-reference" (or, pass-by-reference for this post, I assume it's the same thing) (as in your example). So, would you say that in C++ and in C# (using ref) these are examples of true pass-by-reference? And C's way of passing by pointer is not (because pointer values are copied). – huggie Dec 17 '14 at 00:53
  • 1
    What is in the names anyway? In the C++ and C# the implementation, isn't it that the address, whether itself is copied onto the function local variable stack or are just in it by moving the stack pointer, referring to a piece of memory, whether on stack or on heap? I don't see any necessary underlying difference in implementation other than the syntax. Is "pass-by-reference" vs. passing pointers merely the conceptual difference at a programming level, but not the underlying mechanism of how variable passing is done? – huggie Dec 17 '14 at 00:55
  • @huggie Not exactly. If I did this: "void (int& x) { x = 0; }" i would be changing the value of x from where it was called. If I instead did this: "void (int* x) { x = 0; }" i would only change the local variable x. – Mark Walsh Oct 21 '18 at 04:05
18

The concept of "reference semantics" is an abstract, theoretical one, meaning that you have a way for a function to modify existing data in place by giving that function a reference to the data.

The question is whether reference semantics can be implemented by a language. In C, you can obtain reference semantics by using pointers. That means you can obtain the desired behaviour, but you need to assemble it yourself from various bits and pieces (pointer types, address-of operators, dereference operators, passing pointers as function arguments ("by value")).

By contrast, C++ contains a native reference type and thus implements reference semantics directly in the language.

Other languages have different approaches to this, for example, in Python many things are references by default (and there's a difference, say, between a = b and a = b[:] when b is a list). Whether and how a language provides reference vs value semantics is a profound part of the language's design.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084