Need Help in Recurssion

Hi there

I got  a problem....as follows

Function1(x as integer)

if (x>=2) then
return function1(x-1) + function1(x-2)
else
return 0
end if

i want to know how many times the function1 will call
and if possible give me some more detailed explanation about calling the function

thanks & regards
Reddy
maramreddyAsked:
Who is Participating?
 
Harisha M GCommented:
Hi,

It depends on the value of x...

When x = 2, it will get called twice
When x = 3, it will get called 2 + 1 = thrice
When x = 4, 3 + 2 = 5 times
When x = 5, 5 + 3 = 8 times
...

---
Harish
0
 
iHadiCommented:
Yes it depends on the value of x but I think the values mgh_mgharish provided

It looks like you're using vb.net (using return)

By the way its a good idea to add a counter to the function to see exactly how many times it is called:

Dim counter As Integer

Private Function Function1(x As Integer)
    If (x >= 2) Then
        Function1 = Function1((x - 1) + Function1(x - 2))
    Else
        Function1 = 0
    End If
    counter = counter + 1
End Function

Private Sub Command1_Click()
    Counter = 0
    Function1 9
    MsgBox Counter
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.