# Recursive function yields undefined

**URL:** <https://forum.kirupa.com/t/recursive-function-yields-undefined/243429>\
**Category:** flash\
**Created:** [November 6, 2007, 10:38pm UTC](https://forum.kirupa.com/t/recursive-function-yields-undefined/243429 "2007-11-06T22:38:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ollu](https://avatars.discourse-cdn.com/v4/letter/o/ee59a6/32.png) [@ollu](https://forum.kirupa.com/u/ollu)\
**Post date:** [November 6, 2007, 10:38pm UTC](https://forum.kirupa.com/t/recursive-function-yields-undefined/243429/1 "2007-11-06T22:38:26Z")

</div>

Hello happy people,

I have this function that I want to summarize number given from an array. If the out put total exceeds the variable maxSum I want the function to run again until it gets an result equals or lower than the maxSum.

Now when I test this I “undefined” as out put when the recursion is triggered. Below is my example code to test from. I hope that someone could help me solve this and hopefully explain what I’ve done wrong …

```php

var myTimes:Number = 3;
var maxSum:Number = 20
var myArray:Array = new Array(6,5,6,9,8,6,6,8,6,9,5,7,8,5,6,8,7,9,7,5,5,8,5,7,5,5,5);

function calcSum(times:Number, array:Array) {
    var outPut:Number = 0;
    var sumarize:Number = 0;
    for (var i:Number = 0; i<times; i++) {
        sumarize = array[random(array.length)];
        outPut += sumarize;
    }
    //Check so that the outPut isn't greater then the maxSum
    if (outPut > maxSum || outPut == undefined) {
        calcSum(random(myTimes)+1, array);
    } else {
        return outPut;
    }
}

my_btn.onRelease = function () {
    trace(calcSum(myTimes, myArray));
}

```

kind regards, Ollu
