# I am mystified by ObjC's message syntax

**URL:** <https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134>\
**Category:** Factual Questions\
**Created:** [May 2, 2004, 10:49am UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134 "2004-05-02T10:49:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 2, 2004, 10:49am UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/1 "2004-05-02T10:49:52Z")

</div>

I’ve done lots of C programming and OOP of various flavors (Java and Perl mostly) so when it comes to passing arguments, I’m used to sticking them in a parenthesized list after the function/method name.

This doesn’t appear to be what you do with Objective C method calls, though. Here’s an example from my book:

```auto

[rectangle setWidth:width]

```

This, apparently, calls the **setWidth** method on the object **rectangle** , and passes the value of **width** as the argument. Makes sense so far. But then they show me this:

```auto

[rectangle setWidth:width height:height]

```

This is where I get lost. Apparently, this method is called **setWidth:height** , yet the **width** argument is stuck right in the middle of the method name (WTF?). It feels like they’re trying to do named parameters in a syntax that wasn’t exactly designed for it, or something. I don’t understand where the method names are ending and the arguments are beginning. Why is it called setWidth if it takes both a width and a height? And what’s the syntax for a method with more than two arguments? (For example, something that takes a width, height and x,y offset). Unfortunately, the book does not go into more detail after this example.

😕

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 2, 2004, 5:23pm UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/2 "2004-05-02T17:23:56Z")

</div>

Bu-bump beedump.

---

<div class="post-metadata">

**Author:** ![Balthisar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/balthisar/32/2825_2.png) [@Balthisar](https://boards.straightdope.com/u/Balthisar)\
**Post date:** [May 2, 2004, 6:22pm UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/3 "2004-05-02T18:22:10Z")

</div>

> [@friedo](#):
>
> I’ve done lots of C programming and OOP of various flavors (Java and Perl mostly) so when it comes to passing arguments, I’m used to sticking them in a parenthesized list after the function/method name.
> 
> This doesn’t appear to be what you do with Objective C method calls, though. Here’s an example from my book:
> 
> ```auto
> 
> [rectangle setWidth:width]
> 
> ```
> 
> This, apparently, calls the **setWidth** method on the object **rectangle** , and passes the value of **width** as the argument. Makes sense so far. But then they show me this:
> 
> ```auto
> 
> [rectangle setWidth:width height:height]
> 
> ```
> 
> This is where I get lost. Apparently, this method is called **setWidth:height** , yet the **width** argument is stuck right in the middle of the method name (WTF?). It feels like they’re trying to do named parameters in a syntax that wasn’t exactly designed for it, or something. I don’t understand where the method names are ending and the arguments are beginning. Why is it called setWidth if it takes both a width and a height? And what’s the syntax for a method with more than two arguments? (For example, something that takes a width, height and x,y offset). Unfortunately, the book does not go into more detail after this example.
> 
> 😕

Yeah, this bugs me sometimes to. Try to think of it like this… you’ve got your object **rectangle** , which has a method called **setWidth:**. You’ve also got _another_ method called **setWidth:** that exhibits a type of polymorphism or function overriding that allows you to also set the height.

In your class, you’re really defining two methods, **setWidth:** and **setWidth:height**. It’s a convenience when you start to implement. You can choose (dynamically, even) to use whichever method you want, using the same name.

The alternative is to always use the only a **setWidth:** and a separate **setHeight:** method. Objective-C being as flexible as it is, it would soon be a major pain in the butt to constantly refer to the same object over and over again to call multiple methods.

---

<div class="post-metadata">

**Author:** ![Balthisar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/balthisar/32/2825_2.png) [@Balthisar](https://boards.straightdope.com/u/Balthisar)\
**Post date:** [May 2, 2004, 6:28pm UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/4 "2004-05-02T18:28:46Z")

</div>

I guess I didn’t answer the rest of everything, though.

In your example, the syntax would be something like:

setWidth:height:xOffset:yOffset, and you’d call it like this:

[rectangle setWidth:w height:h xOffset:x yOffset:y]

Presumably, there’s be “overridden” methods like:

setWidth:height  
setWidth:  
xOffset  
yOffset  
xOffset:yOffset

Essentially, you’re given the same group of “messages” that can be sent to the object at the same time, as long as you keep your naming consistent. In reality, though, they’re all distinct messages.

As to where method names and arguments begin and end, well, like in C, _all_ methods take arguments, even if void. So the “method name” is really also the first argument. Then the colons spell out the rest of them. This is probably where you’re most confused, so I’ll say it again: _the method name is really the name of the first argument_.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 3, 2004, 10:28am UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/5 "2004-05-03T10:28:50Z")

</div>

> [@Balthisar](#):
>
> _the method name is really the name of the first argument_.

Thanks, that’s beginning to make more sense to me. I really like Objective C so far, so I’m going to have to get used to this weirdness. Oh well.

---

<div class="post-metadata">

**Author:** ![Balthisar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/balthisar/32/2825_2.png) [@Balthisar](https://boards.straightdope.com/u/Balthisar)\
**Post date:** [May 4, 2004, 12:49am UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/6 "2004-05-04T00:49:15Z")

</div>

Are you doing Cocoa or something on Linux?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 4, 2004, 12:58am UTC](https://boards.straightdope.com/t/i-am-mystified-by-objcs-message-syntax/243134/7 "2004-05-04T00:58:53Z")

</div>

Cocoa. I’m working through the examples in the _Cocoa with Objective C_ book right now.
