Functions within functions

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


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(!)