# Functions within functions

**URL:** <https://forum.kirupa.com/t/functions-within-functions/329564>\
**Category:** Uncategorized\
**Created:** [September 15, 2012, 12:42am UTC](https://forum.kirupa.com/t/functions-within-functions/329564 "2012-09-15T00:42:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hederacea](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@Hederacea](https://forum.kirupa.com/u/Hederacea)\
**Post date:** [September 15, 2012, 12:42am UTC](https://forum.kirupa.com/t/functions-within-functions/329564/1 "2012-09-15T00:42:50Z")

</div>

Hi Everyone - I’m doing a subject on flash at uni, and one of the tutorials works step by step through this timeline code:

```auto

import flash.display.MovieClip;
// more imports...
var ship:MovieClip;
var speed:Number;
var monster:uint;
var monsterTimer:Timer;
var monsterStorage:MovieClip;

function init():void
{
    ship=new Ship;
    //lines of code....
    monsterTimer.addEventListener(TimerEvent.TIMER,createMonster);
    monsterTimer.start();
    stage.addEventListener(KeyboardEvent.KEY_DOWN,pressKey);
    function pressKey(event:KeyboardEvent):void
    {
        /// lines of code.....
    }
    
    function createMonster(event:Event):void
    {
        // lines of code.....
        
        addEventListener(Event.ENTER_FRAME,killEnemy);
        
        function killEnemy(event:Event):void
        {
            if(ship.hitTestObject(monsterNow))
            {
                monsterNow.parent.removeChild(monsterNow);
                removeEventListener(Event.ENTER_FRAME,killEnemy);
            }
        }
    }
}

init();

```

I tried to help someone with a variant of this code they were using for their assignment but I just don’t understand why there are functions within functions - it seems a very messy way of coding and I found it impossible to debug. I’ve already done a few programming subjects (Java and C++) but just I don’t get this…  
Is there a good reason for programming like this that I’m missing?

Informed opinions would be much appreciated(!)
