Compute nth Fibonacci number
Visual Basic.NET, like most languages, permits recursion, a process by which a function calls itself under certain conditions.
Here is a basic function in Visual Basic .NET to compute Fibonacci numbers.
This function works by first checking if the function has been called with the parameter
n equal to
2. By definition, the first two values in the Fibonacci sequence are 1 and 1, so no further computation is necessary to determine this. If
n is greater than 2, we cannot look up the associated value as easily, but we know that any such Fibonacci number is equal to the sum of the prior two numbers, so we request those via recursion (calling our own Fibonacci function). Since successive recursive calls get called with smaller and smaller numbers via decrements of -1 and -2, we know that eventually they will reach numbers that are smaller than 2. Once those conditions (called base cases) are reached, the stack unwinds and we get our final result.