Concepts of Programming Languages

(Sean Pound) #1
11.4 Language Examples 491

The syntactic form of the formal parameter list is different from that of
the more common languages, C, C++, Java, and C#. If there is one parameter,
its type is specified in parentheses before the parameter’s name, as in the fol-
lowing method prototype:


-(void) meth1: (int) x;


This method’s name is meth1: (note the colon). A method with two parameters
could appear as in the following example method prototype:


-(int) meth2: (int) x second: (float) y;


In this case, the method’s name is meth2:second:, although that is obviously
a poorly chosen name. The last part of the name (second) could have been
omitted, as in the following:


-(int) meth2: (int) x: (float) y;


In this case, the name of the method is meth2::.
Method definitions are like method prototypes except that they have a
brace-delimited sequence of statements in place of the semicolon.
The syntax of a call to a method with no parameters is as follows:


[object-name method-name];


If a method takes one parameter, a colon is attached to the method name
and the parameter follows. There is no other punctuation between the method
name and the parameter. For example, a call to a method named add1 on the
object referenced by myAdder that takes one parameter, in this case the lit-
eral 7 , would appear as follows:


[myAdder add1: 7];


If a method takes two parameters and has only one part to its name, a colon
follows the first parameter and the second parameter follows that. No other
punctuation is used between the two parameters. If there are more parameters,
this pattern is repeated. For example, if add1 takes three parameters and has
no other parts to its name, it could be called with the following:


[myAdder add1: 7: 5: 3];


A method could have multiple parameters and multiple parts to its name,
as in the previous example:


-(int) meth2: (int) x second: (float) y;


An example call to this method follows:


[myObject meth2: 7 second: 3.2];

Free download pdf