10

Since we always use pointers to define variables, I was wondering if Objective-C is "pass by value", since like Java, the actual value would be passed by using its reference.

However, since it seems to be built up on top of C, would it have all the functionality of C?

jscs
  • 63,694
  • 13
  • 151
  • 195
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • 2
    Like C it's pass-by-value. You can, of course, simulate pass-by-reference by using `&` with a parameter, but it is just simulation. – Hot Licks Mar 06 '14 at 02:10

2 Answers2

24

C does not support pass-by-reference and Objective-C, being a strict superset of C doesn't either.

In C (and Objective-C) you can simulate pass-by-reference by passing a pointer, but it's important to remember that you're still technically passing a value, which happens to be a the value of a pointer.

So, in Objective-C (and C, for the matter) there is no concept of reference as intended in other languages (such as C++ or Java).

This can be confusing, so let me try to be clearer (I'll use plain C, but - again - it doesn't change in Objective-C)

void increment(int *x) {
   *x++;
}

int i = 42;
increment(&i); // <--- this is NOT pass-by-reference.
               //      we're passing the value of a pointer to i

On the other hand in C++ we could do

void increment(int &x) {
   x++;
}

int i = 41;
increment(i); // <--- this IS pass-by-reference
              //      doesn't compile in C (nor in Objective-C)
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • 2
    This is not true. When you do `somefunction(&var);` var is being passed by reference. – Merlevede Mar 06 '14 at 01:55
  • 8
    No it's not. You're still passing the value of a pointer to `var`. It's like doing `void *p = &var; somefunction(p);` – Gabriele Petronella Mar 06 '14 at 01:56
  • 1
    I still disagree. `function(p)` passes the value of `p`, but `function(&var)` passes the **address** of `var`. – Merlevede Mar 06 '14 at 01:58
  • @Merlevede to convince yourself, write down the signature of `function`. Isn't it `function(void *x)`? – Gabriele Petronella Mar 06 '14 at 01:59
  • @Merlevede, by your own definition, Objective-C is not pass by reference. There is no difference between function(p) and function(&var). – Chris McGrath Mar 06 '14 at 02:08
  • @Merlevede nope. You fail to understand that if you pass `&var`, you're passing the address of `var`, which **cannot** be modified by the function (because is pass-by-value). What can be modified is `var` (by dereferencing the pointer), which is **not** the argument of the function. – Gabriele Petronella Mar 06 '14 at 02:09
  • 1
    @merlevede I added a C++ example to illustrate the difference. C++ has real references, as Java does. You can pass a variable and the inner function gets access to a reference. In C you can simulate this by _passing a reference_ (i.e. a pointer), which is different from _passing by reference_. – Gabriele Petronella Mar 06 '14 at 02:18
  • 2
    I had to go and get my Kernigham & Richie (inventors of C) book. You're right, I apologize!!!! – Merlevede Mar 06 '14 at 02:19
  • 2
    @Merlevede no problem, it's rather subtle as a difference. I'm glad we sorted this out. – Gabriele Petronella Mar 06 '14 at 02:22
  • References is the same pointers at binary level. They just have some conveniences and restrictions for programmes. For example you don't need dereference them every time. – Cy-4AH Mar 06 '14 at 06:36
  • 2
    @Cy4AH, agreed, but from a higher level point of views that makes a whole lot of a difference. For instance, references are immutable, i.e. they don't support pointer arithmetic. The fact that Java only supports references and not pointers is what allows it to be managed language, for example. – Gabriele Petronella Mar 06 '14 at 09:41
  • 2
    @GabrielePetronella: "C++ has real references, as Java does." Java does NOT have pass-by-reference. In Java, "reference" is defined as pointer to an object, which is also what "reference" means in most languages. "C++ reference" is something else entirely. – newacct Mar 18 '14 at 05:32
  • So the old terms pass-by-value and pass-by-reference are themselves confusing to people. Agree with newacct. Those terms don't make sense in Objective-C since all pointers to objects and all ivars are called references. – uchuugaka Mar 18 '14 at 07:37
  • @newacct Java *only* has pass-by-reference for object types (and pass-by-value for native types). As I previously said, a reference is just a pointer with no arithmetic. Java simply doesn't allow regular pointers, for the sake of memory management. – Gabriele Petronella Mar 19 '14 at 16:08
  • 1
    @GabrielePetronella: Nope. Java has NO pass-by-reference. (Just look around the Internet or Stack Overflow.) And Java has no "object types". The only types in Java are primitive types and reference types. – newacct Mar 20 '14 at 03:43
1

It is a strict superset of C. It does the same as C. It's one reason all Objects are actually pointers to structs.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55