Need Help in Recurssion

Posted on 2006-03-26
Last Modified: 2012-08-13
Hi there

I got  a follows

Function1(x as integer)

if (x>=2) then
return function1(x-1) + function1(x-2)
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
Question by:maramreddy
    LVL 37

    Accepted Solution


    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

    LVL 13

    Assisted Solution

    Yes it depends on the value of x but I think the values mgh_mgharish provided

    It looks like you're using (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))
            Function1 = 0
        End If
        counter = counter + 1
    End Function

    Private Sub Command1_Click()
        Counter = 0
        Function1 9
        MsgBox Counter
    End Sub

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now