# Recursive function with return value

**URL:** <https://forum.kirupa.com/t/recursive-function-with-return-value/264980>\
**Category:** flash\
**Created:** [July 2, 2008, 6:18pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980 "2008-07-02T18:18:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nburlington](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nburlington/32/1365_2.png) [@nburlington](https://forum.kirupa.com/u/nburlington)\
**Post date:** [July 2, 2008, 6:18pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980/1 "2008-07-02T18:18:11Z")

</div>

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

```auto
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();

```

---

<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 2, 2008, 6:35pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980/2 "2008-07-02T18:35:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nburlington](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nburlington/32/1365_2.png) [@nburlington](https://forum.kirupa.com/u/nburlington)\
**Post date:** [July 2, 2008, 7:03pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980/3 "2008-07-02T19:03:53Z")

</div>

Right, that was stupid.

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

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 2, 2008, 8:21pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980/4 "2008-07-02T20:21:20Z")

</div>

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.

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![nburlington](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nburlington/32/1365_2.png) [@nburlington](https://forum.kirupa.com/u/nburlington)\
**Post date:** [July 2, 2008, 8:33pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980/5 "2008-07-02T20:33:38Z")

</div>

its just a hypothetical example.

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 2, 2008, 9:04pm UTC](https://forum.kirupa.com/t/recursive-function-with-return-value/264980/6 "2008-07-02T21:04:57Z")

</div>

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.

```auto
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);

```
