recursive function fails to return value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lacalacaloca1
    New Member
    • Oct 2007
    • 1

    recursive function fails to return value

    [CODE=vb]Function recursiveTest(n )
    x = n + 1
    Debug.Print n
    If x > 10 Then
    recursiveTest = "finish recursive test"
    Else
    recursiveTest x
    End If
    End Function[/CODE]

    If I debug, I don't see the "finish recursive test".
    ? recursiveTest(7 ,false)
    7
    8
    9
    10
    <blank>

    I should see "finish recursive test" instead of the whilte blank line there.
    Last edited by Killer42; Oct 25 '07, 01:25 AM.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by lacalacaloca1
    ...
    i should see "finish recursive test" instead of the whilte blank line there.
    Try to define better the variables you're using, I hope your problem could solve if you define, and evaluate strings.
    [CODE=vb]
    function recursiveText( byval n as string) as string
    dim x as double
    x = val(n) +1 [/CODE]

    ... and everything else. Give it a try

    HTH

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by lacalacaloca1
      ? recursiveTest(7 ,false)
      7
      8
      9
      10
      <blank>

      I should see "finish recursive test" instead of the whilte blank line there.
      Are you sure the Debug output you're showing us matches the code listed above? You have two parameters there, while the function declaration has only one. Surely that should produce an error.

      Also, there's no reason why you should see that text come back. Only the last "recursion" returns that value. It is then discarded when you "back out" to the next level. In other words, when it passes 10, it returns your string. The next level up, which called it, simply returns then without setting any return value. Try this on line 7, and see what happens...

      [CODE=vb]recursiveTest = recursiveTest(x )[/CODE]
      Last edited by Killer42; Oct 25 '07, 01:29 AM.

      Comment

      Working...