We help IT Professionals succeed at work.

Need Help in Recurssion

maramreddy
maramreddy asked
on
Medium Priority
211 Views
Last Modified: 2012-08-13
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
Comment
Watch Question

Engineer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.