4.1.1.15–4.1.1.16 - Stack frames and recursion (VB.NET)

4.1.1.15–4.1.1.16 - Stack frames and recursion (VB.NET)

Learn how a call preserves its own state, how recursive calls reach a base case and how their results return in reverse order. VB.NET tasks assess written source and traces; this route is incomplete: runnable support pending.

Call stack frames

Every unfinished subroutine call needs a record of where to resume and which data belongs to it. A stack frame is that call's record, containing its return address, parameter values and local variables. The return address identifies the next instruction in the caller, not the data value returned by a function.

A call stack uses last in, first out. Calling a routine pushes a frame; returning removes that frame and resumes its caller. If Main calls A, which calls B, B must finish before A resumes. While B is active, A's frame still preserves its parameters and locals. A compiler/runtime may optimise the physical representation; this conceptual model explains the syllabus mechanism.

VB.NET
Module Program
    Function DoubleIt(ByVal value As Integer) As Integer
        Dim result As Integer = value * 2
        Return result
    End Function

    Function AddDouble(ByVal number As Integer) As Integer
        Dim extra As Integer = 3
        Dim answer As Integer = DoubleIt(number) + extra
        Return answer
    End Function

    Sub Main()
        Console.WriteLine(AddDouble(4))
    End Sub
End Module

While DoubleIt is active, its frame has value = 4 and eventually result = 8. Beneath it, AddDouble retains number = 4, extra = 3 and a return address in Main. DoubleIt returns 8 to the unfinished addition in AddDouble, whose answer becomes 11. Then AddDouble returns 11 to Main. The frame is removed; the returned value is retained by the caller as needed.

Base and general cases

Recursion occurs when a routine calls itself, directly or through another routine. Each invocation is a separate call with its own frame. The source is reused; the active parameter and local values are not one shared copy.

The base case solves a small instance without another recursive call. The general case expresses a larger instance using a smaller one. Correct termination needs both a reachable base case and progress towards it for every input allowed by the contract.

VB.NET
Function Triangle(ByVal n As Integer) As Integer
    If n = 0 Then
        Return 0
    End If
    Return n + Triangle(n - 1)
End Function

For a guaranteed non-negative small Integer, the function sums 1 through n. Triangle(3) needs 3 + Triangle(2), which needs 2 + Triangle(1), which needs 1 + Triangle(0). The last call returns 0. The waiting additions then produce 1, 3 and 6. Calls descend towards the base case; answers unwind back towards the original caller.

Active call on descentIts unfinished workResult on return
Triangle(3)add 3 to child result6
Triangle(2)add 2 to child result3
Triangle(1)add 1 to child result1
Triangle(0)base case, no child0

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account