<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Richard Lord &#187; Functions</title>
	<atom:link href="http://www.richardlord.net/blog/tag/functions/feed" rel="self" type="application/rss+xml" />
	<link>http://www.richardlord.net</link>
	<description>Actionscript/Flex, PHP and Java developer</description>
	<lastBuildDate>Thu, 26 Aug 2010 17:32:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>The parentheses operator</title>
		<link>http://www.richardlord.net/blog/the-parentheses-operator</link>
		<comments>http://www.richardlord.net/blog/the-parentheses-operator#comments</comments>
		<pubDate>Wed, 16 Jul 2008 08:10:57 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Event handlers]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Learning]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/the-parentheses-operator/</guid>
		<description><![CDATA[<p>It's not uncommon for less experienced Actionscript developers, particularly self-taught developers, to be a little confused about the purpose of the parentheses you put after a function name when calling the function. The most common question is why aren't the parentheses used when assigning a function as an event handler?...</p>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not uncommon for less experienced Actionscript developers, particularly self-taught developers, to be a little confused about the purpose of the parentheses you put after a function name when calling the function. The most common question is why aren&#8217;t the parentheses used when assigning a function as an event handler? i.e. Why are there no parentheses after clickHandler in the first line of
this code?</p>

<pre class="code">addEventListener( MouseEvent.CLICK, clickHandler );

function clickHandler( event:MouseEvent ):void
{
	// function body here
}</pre>

<h3>What the documentation says</h3>

<p>The (usually excellent) Flash and Flex help isn&#8217;t much help here. In the Actionscript 3 language reference, it states that the parentheses &quot;Surrounds one or more parameters and passes them to the function that precedes the parentheses&quot;, which doesn&#8217;t explain why (or even if) the parentheses are necessary when no parameters are passed to the function.</p>

<p>Elsewhere in the help, it states that &quot;If you are calling a function with no parameters, you must use an empty pair of parentheses&quot;. No explanation &#8211; just do it. Which inevitably leads to developers wondering why they mustn&#8217;t do it when assigning event handlers.</p> 

<p>Colin Moock&#8217;s Essential Actionscript 3 (also usually excellent) takes a similar approach, simply stating &quot;Notice the important and mandatory use of the parentheses operator following the method name&quot;.</p>

<p>People learn things better if they understand them, so here&#8217;s my attempt at an explanation. If you&#8217;re confused about when to use the parentheses operator this should help. If not, come back tomorrow when I&#8217;ll post something a lot more advanced about programmatic skins in Flex.</p>

<h3>What the parentheses operator does</h3>

<p>A function is a portion of code that performs a specific task or set of tasks within your application. The function may be something you have defined, or it may be an intrinsic function (a function built-in to the flash player, like the trace function). When a function is a property of an object, it&#8217;s usually called a method. Here&#8217;s a trivial example function definition.</p>

<pre class="code">function sayHello():void
{
	someTextField.text = "Hello";
}</pre>

<p>The code inside the function is run by calling the function, like this.</p>

<pre class="code">sayHello();</pre>

<p>The parentheses could perhaps be better named as the function calling operator since that is what they do. We use the function&#8217;s name to refer to the function, and then we use the parentheses to run the function we have identified.</p>

<p>If the function needs some additional information in order to run (the parameters) then that information goes inside the parentheses.</p>

<pre class="code">function saySomething( thingToSay:String ):void
{
	someTextField.text = thingToSay;
}

saySomething( "This is a boring example" );</pre>

<p>But the parentheses, with or without parameters, are the operator that calls the function.</p>

<h3>What happens without the parentheses</h3>

<p>If that&#8217;s what the parentheses are for, what happens if we leave them out? Like this</p>

<pre class="code">sayHello;</pre>

<p>Well, first of all, you won&#8217;t get an error. sayHello exists (it&#8217;s defined above) so the code is valid. But, it doesn&#8217;t do anything. We have used the name of the function (&#8220;sayHello&#8221;) to refer to the function, then we&#8217;ve done nothing with it. When you put the parenthesis after the function you are indicating that you want to call the function now. Without the parenthesis, you&#8217;re not running the function. But calling a function is not the only thing we can do with it.</p>

<p>For example, we can assign it to a variable, like this</p>

<pre class="code">var myFunction:Function = sayHello;</pre>

<p>Now we can refer to the function via the variable. So if we used the parenthesis operator on the variable, it would call the function, like this</p>

<pre class="code">myFunction();</pre>

<p>Or we could continue to use it&#8217;s original name, like this</p>

<pre class="code">sayHello();</pre>

<h3>Event handlers</h3>

<p>When we set up an event handler, we don&#8217;t want to call the function straight away, we want the event handler to call the function later, when the event happens. So, we define an appropriate function</p>

<pre class="code">function clickHandler( event:MouseEvent ):void
{
	someTextField.text = "The mouse was clicked";
}</pre>

<p>Then we assign the function as a listener for the event</p>

<pre class="code">addEventListener( MouseEvent.CLICK, clickHandler );</pre>

<p>When we assign the function as a listener, we don&#8217;t want to call the function, so we don&#8217;t use the parentheses operator. Later, when the event actually happens, the flash player will call the function for us. There&#8217;s some code inside the EventDispatcher class that uses the parentheses operator to call the function when the event happens.</p>

<p>If you make the mistake of using the parentheses operator when assigning the event handler, like this</p>

<pre class="code">addEventListener( MouseEvent.CLICK, clickHandler() );</pre>

<p>then the event handler will be called immediately (you&#8217;ve told it to do so by using the parentheses). This will usually generate an error since you haven&#8217;t assigned a parameter for the event argument.</p>

<p>So that&#8217;s the parenthesis operator. Personally, I like the way that C++ names the parentheses operator the &quot;function operator&quot; &#8211; it&#8217;s the operator that calls a function.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/the-parentheses-operator/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
