''' <summary>''' Gets the n'th Fibonacci number''' </summary>''' <param name="n">The 1-indexed ordinal number of the Fibonacci sequence that you wish to receive. Precondition: Must be greater than or equal to 1.</param>''' <returns>The nth Fibonacci number. Throws an exception if a precondition is violated.</returns>PublicSharedFunction Fibonacci(ByVal n asInteger)asIntegerIf n<1ThrowNew ArgumentOutOfRangeException("n must be greater than or equal to one.")EndIfIf(n=1)or(n=2)''Base case. The first two Fibonacci numbers (n=1 and n=2) are both 1, by definition.Return1EndIf''Recursive case.''Get the two previous Fibonacci numbers via recursion, add them together, and return the result.Return Fibonacci(n-1)+ Fibonacci(n-2)EndFunction
This function works by first checking if the function has been called with the parameter n equal to 1 or 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.