Screenwriter, Choreographer, Game Developer

The parentheses operator

Posted on

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? i.e. Why are there no parentheses after clickHandler in the first line of this code?

addEventListener( MouseEvent.CLICK, clickHandler );

function clickHandler( event:MouseEvent ):void
	// function body here

What the documentation says

The (usually excellent) Flash and Flex help isn't much help here. In the Actionscript 3 language reference, it states that the parentheses "Surrounds one or more parameters and passes them to the function that precedes the parentheses", which doesn't explain why (or even if) the parentheses are necessary when no parameters are passed to the function.

Elsewhere in the help, it states that "If you are calling a function with no parameters, you must use an empty pair of parentheses". No explanation - just do it. Which inevitably leads to developers wondering why they mustn't do it when assigning event handlers.

Colin Moock's Essential Actionscript 3 (also usually excellent) takes a similar approach, simply stating "Notice the important and mandatory use of the parentheses operator following the method name".

People learn things better if they understand them, so here's my attempt at an explanation. If you're confused about when to use the parentheses operator this should help. If not, come back tomorrow when I'll post something a lot more advanced about programmatic skins in Flex.

What the parentheses operator does

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's usually called a method. Here's a trivial example function definition.

function sayHello():void
	someTextField.text = "Hello";

The code inside the function is run by calling the function, like this.


The parentheses could perhaps be better named as the function calling operator since that is what they do. We use the function's name to refer to the function, and then we use the parentheses to run the function we have identified.

If the function needs some additional information in order to run (the parameters) then that information goes inside the parentheses.

function saySomething( thingToSay:String ):void
	someTextField.text = thingToSay;

saySomething( "This is a boring example" );

But the parentheses, with or without parameters, are the operator that calls the function.

What happens without the parentheses

If that's what the parentheses are for, what happens if we leave them out? Like this


Well, first of all, you won't get an error. sayHello exists (it's defined above) so the code is valid. But, it doesn't do anything. We have used the name of the function ("sayHello") to refer to the function, then we'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're not running the function. But calling a function is not the only thing we can do with it.

For example, we can assign it to a variable, like this

var myFunction:Function = sayHello;

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


Or we could continue to use it's original name, like this


Event handlers

When we set up an event handler, we don'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

function clickHandler( event:MouseEvent ):void
	someTextField.text = "The mouse was clicked";

Then we assign the function as a listener for the event

addEventListener( MouseEvent.CLICK, clickHandler );

When we assign the function as a listener, we don't want to call the function, so we don't use the parentheses operator. Later, when the event actually happens, the flash player will call the function for us. There's some code inside the EventDispatcher class that uses the parentheses operator to call the function when the event happens.

If you make the mistake of using the parentheses operator when assigning the event handler, like this

addEventListener( MouseEvent.CLICK, clickHandler() );

then the event handler will be called immediately (you've told it to do so by using the parentheses). This will usually generate an error since you haven't assigned a parameter for the event argument.

So that's the parenthesis operator. Personally, I like the way that C++ names the parentheses operator the "function operator" - it's the operator that calls a function.

Share this post or a comment online -

Also in the collection Actionscript