Title here
Summary here
Our first example demonstrates recursion. Here’s the full source code:
To run the program, save it as RecursionExample.vb
and use the VB.NET compiler:
This example demonstrates two types of recursive functions in VB.NET:
Fact
) that calculates the factorial of a number.Fib
) defined inside the Main
method, which calculates Fibonacci numbers recursively.The Fact
function is a classic example of recursion, calling itself with a smaller argument until it reaches the base case.
The Fib
function is defined as a local function within Main
. This is similar to the concept of closures in some other languages. In VB.NET, local functions can be recursive and can capture variables from the enclosing scope.
Both functions are called with the argument 7, and their results are printed to the console.