# What exactly is an anonymous function?

**URL:** https://forum.kirupa.com/t/what-exactly-is-an-anonymous-function/43029
**Category:** Uncategorized
**Created:** [February 18, 2004, 2:21am UTC](https://forum.kirupa.com/t/what-exactly-is-an-anonymous-function/43029 "2004-02-18T02:21:11Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![trock](https://avatars.discourse-cdn.com/v4/letter/t/e56c9b/32.png) [@trock](https://forum.kirupa.com/u/trock)
#### Post date: [February 18, 2004, 2:21am UTC](https://forum.kirupa.com/t/what-exactly-is-an-anonymous-function/43029/1 "2004-02-18T02:21:11Z")

</div>

Is there a difference between:

[AS]advance = function(){…[/AS]

and

[AS]function advance(){…[/AS]

I believe one of them is known as an anonymous function, but I am not sure which one or why.

Along those same lines… If I am on frame 1 of a movie clip, is there a difference between:

[AS]this.advance = function(){…[/AS]

and

[AS]advance = function(){…[/AS]

Thanks.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 18, 2004, 3:29am UTC](https://forum.kirupa.com/t/what-exactly-is-an-anonymous-function/43029/2 "2004-02-18T03:29:02Z")

</div>

In your first example, the first one is an anonymous function, however as you  
are using it, it makes no difference. A different example will make the reason why it’s called an anonymous function more plain.

Consider the following use of a named function.

```auto

function randomizePosition()
{
    this._x = random(300);
    this._y = random(200);
}

mymovie.onEnterFrame = randomizePosition;
myothermovie.onEnterFrame = randomizePosition;

```

“randomizePosition” is a named function. I can assign it to any number of  
movie clips. If I had used the anonymous style syntax, it would have looked like this.

```auto

mymovie.onEnterFrame = function ()
{
    this._x = random(300);
    this._y = random(200);
}

// this is kind of uglY:
myothermovie.onEnterFrame = mymovie.onEnterFrame;

```

As you can see, it’s anonymous because the function wasn’t assigned a name (“randomizePosition”). This syntax is a little uglier when you’re going to be assigning the function to multiple movies. However, for one-shot movies, anonymous functions are fine, and produce slightly smaller code.

In terms of performance, it doesn’t really matter which method you choose.

My personal preference is to use named functions because most of my programming life I’ve been programming in languages which only support that kind (C for example).

- Jim
