# Passing Parameters to Functions - Looking for good explanation

**URL:** <https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438>\
**Category:** flash\
**Created:** [July 8, 2008, 9:53pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438 "2008-07-08T21:53:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 8, 2008, 9:53pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/1 "2008-07-08T21:53:49Z")

</div>

Just trying to pass a parameter in my event handler to a function.

1. Why can’t I do this easily anymore
2. I haven’t been able to find any good threads on the “proper” way to do this in AS

TIA

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 8, 2008, 9:54pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/2 "2008-07-08T21:54:46Z")

</div>

1. Someone decided that the idea behind the new event model was better, but you could try something that duncanhall made: [http://www.kirupa.com/forum/showthread.php?t=291250](http://www.kirupa.com/forum/showthread.php?t=291250)

It latches onto the existing event system to add the sort of functionality that I think you want.

1. As TheCanadian suggests in that thread, the “proper” way could be to make a custom event class and add in special properties to the event that gets passed into the handler. Otherwise, you would have to make sure that some properties of the handler’s enclosing class would be able to give the handler access to the variables that it needs.

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 8, 2008, 10:04pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/3 "2008-07-08T22:04:08Z")

</div>

```auto

someBtn.addEventListener (MouseEvent.MOUSE_DOWN, doSomething);
private function doSomething(e:Event):void {
// i want to have a parameter passed in here from the event handler
}

```

Which I used to be able to do like this

```auto

btn.onPress = function(){
doSomething("example");
}

function doSomething(e:String){
trace(e);
}

```

I was just reading a post of your but didn’t understand it to be honest.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 8, 2008, 10:10pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/4 "2008-07-08T22:10:32Z")

</div>

I edited my first post after (I think) you edited yours, so it has some more info now.

> [@Digitalosophy](#):
>
> Which I used to be able to do like this

You can do something a lot like that in AS3:

```auto
btn.addEventListener(MouseEvent.MOUSE_DOWN, function():void {
	doSomething("example");  
});

function doSomething(e:String){
	trace(e);
}

```

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 8, 2008, 10:12pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/5 "2008-07-08T22:12:46Z")

</div>

Thank you ever so much.

:kommie:

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 8, 2008, 10:21pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/6 "2008-07-08T22:21:29Z")

</div>

You can still do the above now:

```auto
someBtn.addEventListener (MouseEvent.MOUSE_DOWN, function (event:Event):void {
     doSomething("example");
});
private function doSomething(msg:String):void {
     // i want to have a parameter passed in here from the event handler
}

```

Of course you’ve lost access to your listener and would be hard pressed to try and remove it if you needed to.

For more, see:  
[http://www.actionscript.org/forums/showthread.php3?t=172242#post750911](http://www.actionscript.org/forums/showthread.php3?t=172242#post750911)

[edit]Argh :pirate3: beat to the punch while I was searching for the related post 😃 [/edit]

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 8, 2008, 10:25pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/7 "2008-07-08T22:25:01Z")

</div>

> [@senocular](#):
>
> Of course you’ve lost access to your listener and would be hard pressed to try and remove it if you needed to.
> 
> For more, see:  
> [DEWA89 | LINK ALT DAFTAR DAN LOGIN VPN89.ME/DEWA89](http://www.actionscript.org/forums/s...242#post750911)

A notable point in that thread:

> [@senocular@actionscript.org](#):
>
> Of course [that] could easily be avoided by saving your wrapper functions to a variable or array before giving them to addEventListener.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 8, 2008, 10:26pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/8 "2008-07-08T22:26:00Z")

</div>

also often easier said than done 😉

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 8, 2008, 10:29pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/9 "2008-07-08T22:29:22Z")

</div>

I’m a bit skeptical of that claim… or maybe we have different opinions of difficulty. When is it hard to store a reference to a function?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 8, 2008, 10:33pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/10 "2008-07-08T22:33:01Z")

</div>

[QUOTE=Krilnon;2355455]I’m a bit skeptical of that claim… or maybe we have different opinions of difficulty. When is it hard to store a reference to a function?[/QUOTE]

in a loop, or when they’re being created dynamically at different times. While it might be easy to actually make the reference, keeping track of it when you need to remove it could be a problem, this especially if you’re making an unknown number of functions for an unknown number of objects or even reactions to an object (or multiple objects).

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 8, 2008, 10:45pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/11 "2008-07-08T22:45:18Z")

</div>

It seems to me that, in one of those situations, the most complicated part of the code still wouldn’t be storing or finding the function reference, it would be the “making an unknown number of functions for an unknown number of objects or even reactions to an object (or multiple objects)”, or even just figuring out which object you wanted to remove which behavior from that would be the relatively difficult part. If you had that set up and knew the object and the behavior, then it would be simple enough to have some data structure that would take those pieces of information and resolve them into a function reference (because you have those two pieces of info when you create the function anyway).

I don’t really have a good way to assert the ease of doing this other than trying to refute your examples, although I don’t really have a reason to be refuting them in the first place other than that I’ve never run into a situation where it actually is difficult.

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 8, 2008, 10:57pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/12 "2008-07-08T22:57:31Z")

</div>

Making a class that extends event and giving it one-two params vars seems reasonable to me ; )

Not sure what that’s what duncans class does, haven’t check it.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 8, 2008, 11:42pm UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/13 "2008-07-08T23:42:56Z")

</div>

[QUOTE=sekasi;2355464]Making a class that extends event and giving it one-two params vars seems reasonable to me ; ) [/QUOTE]

The problem with that is that it only pertains to events you have control over and dispatch yourself. You run into a problem when you want to add values to say, a MouseEvent which is dispatched by the Flash Player. There’s no way to tell Flash to dispatch MyCustomMouseEvent.

As for what Krilnon brought up:

[QUOTE=Krilnon;2355461] If you had that set up and knew the object and the behavior, then it would be simple enough to have some data structure that would take those pieces of information and resolve them into a function reference (because you have those two pieces of info when you create the function anyway).[/QUOTE]

At that point you’re not doing the function reference thing, at least not in the context for which it was explained. The basic function reference approach is creating a variable and assigning its value to target the function created. This can be extended to target any number multiple functions dynamically by using something like an array. When you start to get into defining structures to handle this, you’re escaping that whole process and getting into a different approach, one not unlike the Color listener example in the previous link. There objects are created to facilitate the listener and the objects it needs to reference. This works both to be able to have listener parameters as well as provide a “persistent” link to the listener function accessible in the listener call itself through this.listener.

So yea. Basically if you have the objects to store data in, do it. If not, make them yourself. Otherwise you might be able to slide on by with some anonymous functions.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 9, 2008, 12:01am UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/14 "2008-07-09T00:01:33Z")

</div>

> [@senocular](#):
>
> At that point you’re not doing the function reference thing, at least not in the context for which it was explained.

Ah. I was just talking about function references that are either standalone or stuck in an arbitrarily complex structure of other objects. I think that this is pretty much a non-issue then. (-:

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [July 9, 2008, 10:06am UTC](https://forum.kirupa.com/t/passing-parameters-to-functions-looking-for-good-explanation/265438/15 "2008-07-09T10:06:38Z")

</div>

Did a blog post on this topic awhile back that might help out with a few different ideas… I didn’t even touch on passing an anonymous function, but I’ve never liked the idea because of the difficulty of removing the listener… There’s 3 other possible solutions though… [http://blog.onebyonedesign.com/?p=60](http://blog.onebyonedesign.com/?p=60)
