Method Arguments

The names you use to refer to a method’s arguments are also local variables. When the method is executed, whatever arguments are passed to the method are copied into these variables. Because the method is dealing with a copy of the arguments, it cannot change the original values passed to the method. This is an important concept. Suppose you had a method called calculate:, defined as follows:

-(void) calculate: (double) x{   x *= 2;   ...}

Also suppose that you used the following message expression to invoke it:

[myData calculate: ptVal];

Whatever value was contained in the variable ptVal would be copied into the local variable x when the calculate method was executed. So changing the value of

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.