Recursive function with return value

How can I have a recursive function that also returns a value. Here’s an example.

public function getNine():Number{

var num:Number;

var myArray:Array = new Array(1,2,3,4,5,6,7,8,9);

if(myArray[0] != 9){
myArray.slice(1);
getNine();
}

return num;
}

var needNine:Number = getNine();

If you were to declare myArray outside of the function, then your function would probably do something more practical.

Right, that was stupid.

Assume that myArray is sitting outside the function. I still need to move through it until I hit 9 though.

What is it you are trying to do with that? If you are just checking if the array has nine in it, a for loop would be easier.

var myArray:Array = new Array(1,2,3,4,5,6,7,8,9);

function getNine(array:Array):int {

    for (var i = 0; i < array.length; i++) {
        if (array* == 9) { return 9; }
    }
    return 0; // did not find 9
}

var needNine:int = getNine(myArray);

its just a hypothetical example.

this will work, but will run forever if the array does not have a 9 in it. its still easier to use other loops instead recursive loops.

var myArray:Array = new Array(1,2,3,4,5,6,7,8,9);

function getNine(array:Array):Number {

	if (array[0] == 9) {
		return 9;
	} else {
		return getNine(array.slice(1));
	}

}

var needNine:Number = getNine(myArray);