0

Possible Duplicate:
What's the difference between passing by reference vs. passing by value?

I read that in C arguments are passed by value, but what's is the difference between passing arguments by value (like in C) or by refencence (like C++ - C#)? What's the difference between a pointer and a reference?

void with_ptr(int *i)
{ *i = 0; }

void with_ref(int &i)
{ i = 0; }

In these cases are modified both value? If yes, why C++ allows to pass arguments by reference? I think it is not clear inside the function that the i value could be modified.

Community
  • 1
  • 1
Nick
  • 10,309
  • 21
  • 97
  • 201
  • 1
    I'd imagine that there are lots of questions on StackOverflow relating directly to this issue. Please check them out first... but yes both values are modified by the functions you give. – Dennis May 15 '12 at 09:44

5 Answers5

10

what's is the difference between passing arguments by value or by reference

If you pass by value, changes to the variable will be local to the function, since the value is copied when calling the function. Modifications to reference arguments will propagate to the original value.

What's the difference between a pointer and a reference?

The difference is largely syntactic, as you have seen in your code. Furthermore, a pointer can be reassigned to point to something else (unless it’s declared const), while a reference can’t; instead, assigning to a reference is going to assign to the referenced value.

I think it is not clear inside the function that the i value could be modified.

On the contrary, it’s absolutely clear: the function signature tells you so.

There’s actually a case to be made that it’s not clear outside the function. That’s why original versions of C# for instance mandated that you explicitly annotate any by-reference calling with ref (i.e. f(ref x) instead of plain f(x)). This would be similar to calling a function in C++ using f(&x) to make it clear that a pointer is passed.

But in recent versions of C#, the use of ref for calling was made optional since it didn’t confer enough of an advantage after all.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    +1 "the function signature tells you so." ... always assume that a function without `const` parameters will modify the parameters. – Dennis May 15 '12 at 09:46
2

Consider this:

1) Passing by reference provides more simple element access i instead of *i

2) Generally you cannot pass null reference to a method, but can pass a null pointer

3) You can't change the address of reference, but can change it for a pointer(although, as pointer itself passed by value, this change will be discarded upon function exit)

Hope, this helped a bit

undefined
  • 1,354
  • 1
  • 8
  • 19
1

Actually, in the first case, you can't modify the argument. The pointer itself is immutable, you can only modify the value it points to.

If yes, why C++ allows to pass arguments by reference?

Because pointers can very easily be miss-used. References should almost always be prefered. For your case, what if you pass a NULL to with_ptr? You'll get undefined behavior, which is not possible if you use with_ref.

I think it is not clear inside the function that the i value could be modified.

It is very clear. If you see a function that takes a parameter by non-const reference, you can assume it will be changed.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

I think that a method can only change an argument's value, if this is passed by reference. If you pass a argument by value in a method, then whatever change you make to its value, this will no be available in the parent method.

mariosk89
  • 944
  • 1
  • 11
  • 31
0

As far as I know, I think the reference is safer to use in a sense that it can't be modified (always points to the same thing), and should be initialized if it's a local variable. Pointer, however, can be change to point to somewhere else.

int x = 10;
int &y = x;
int *p = &x;
p++; //Legal if you know what's next
y++; // Increases the value of x. Now x = y = 11;

As my two cents, I think reference variables are mere alternative names for the same memory address by which it was initialized. This also explains pretty nice: http://www.dgp.toronto.edu/~patrick/csc418/wi2004/notes/PointersVsRef.pdf

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44