We help IT Professionals succeed at work.

on
Medium Priority
211 Views
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

## View Solutions Only

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.

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
##### Thanks for using Experts Exchange.

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