0

let me first say. I know the title suspects that I'm asking a question which was answered here and around the internet many times. I did research indeed yet I just can't find a satisfying answer.

My question in the end comes down to this. Why is Java Call By Value and C++ Call By Reference (when using pointers) ?

Consider a method call in Java when passing references and in c++ when passing pointers. In the end in both cases I am able to make changes which are visible to the caller. Also in both cases I'm passing the address of the object to the function. In fact I'm copying i.e. making a call by value in c++ too when passing a pointer don't I?

You can simply verify that circumstance by running the following code:

#include <iostream>

void modify (int *i) {
    int a = 5;
    i = &a;
}

int main () {
    int b;
    int *i = &b;
    std::cout << i << std::endl;
    modify(i);
    std::cout << i << std::endl;
    return 0;
}

Which will print two times the same address.

For me it's just not enough to justify a call by reference with the property that you can do a swap function with it. I would like to know the very heart of what makes call by reference call by reference.

Thank you in advance.

ben
  • 5,671
  • 4
  • 27
  • 55
  • "Why is Java Call By Value and C++ Call By Reference (when using pointers) ?" Because the people who designed the languages specified them that way. – twalberg Jan 20 '14 at 16:37

2 Answers2

1

My question in the end comes down to this. Why is Java Call By Value and C++ Call By Reference (when using pointers) ?

C++ is really call-by-value, not call-by-reference. The by-reference behavior you see in C++ is a side effect of the fact that you're passing a pointer: the pointer itself is copied by value. You can't compare these situations because Java doesn't have pointers.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • @user2820379 a pointer passed by value can be used to modify the object it points to (but not the pointer), just as when java passes a reference by value, it can be used to modify the object it refers to (but not the reference). – user3125280 Jan 21 '14 at 12:54
  • Sounds perfectly intuitive for me. I'm just confused since C++ is often referred to be supporting call by reference. – ben Jan 21 '14 at 13:01
  • @user2820379 Call by value means *all callee code* see only the 'value' of an object i.e. a copy. Call by reference means *all callee code* sees a reference to an object. You can still pass a reference to an object by value/copy in a call by value language, and pass a reference to a copy/value in a call by reference language. – user3125280 Jan 21 '14 at 13:07
  • I see. Thank you for elaborating on that. – ben Jan 21 '14 at 14:01
  • 1
    @user2820379 C++ *does* support by-reference arguments using the `&` modifier. But this is not the default behavior. – TypeIA Jan 21 '14 at 14:23
  • @dvnrss Although, "under the hood", pass-by-reference in C++ is really just a syntactic convenience for pass-pointer-by-value in most implementations. It's pretty much that way in Java, too, despite not having code-level pointers, or any other language that supports pass-by-reference. At the end of the day, a reference is pretty much the location of an existing object, which is either a pointer, or an offset from some other location, or an index into a table, or something... – twalberg Jan 22 '14 at 13:30
0

The example you give is inappropriate use of call by reference. In the function modify

void modify (int *i) {
    int a = 5;
    i = &a;
}

You declare a local variable a which is on the stack. When it goes out of scope, its address is meaningless. So assigning address of a to i has no effect on what i points to.

So what makes call by reference work is you can modify the value of what i points to in the function. For example:

void modify (int *i) {
    int a = 5;
    *i = a;
}

Therefore in the main function, you can call it like:

int b = 2;
int *i = &b;
std::cout << *i << std::endl;  //This will print 2
modify(i);
std::cout << *i << std::endl;  //This will prints 5
tonga
  • 11,749
  • 25
  • 75
  • 96
  • You are absolutely right. The example I gave had only the purpose to underline that pointers are passed by value. – ben Jan 20 '14 at 19:20