4.1.1.10–4.1.1.12 - Subroutines, parameters and return values (VB.NET)

4.1.1.10–4.1.1.12 - Subroutines, parameters and return values (VB.NET)

Learn to split a program into named routines, pass data through precise interfaces and return results. The examples and marked source answers use VB.NET. This route is incomplete: runnable support pending; these are written code activities without Run, while the real Paper 1 is an onscreen practical examination.

Named subroutines

A subroutine is a named, out-of-line block of code: its definition is separate from the statements that call it. Calling it transfers control into that block; reaching its end returns control to the statement after the call. Writing a definition alone does not execute the body.

In VB.NET, a Sub procedure performs an action without returning a result, while a Function procedure returns a value. Both are subroutines. The term procedure is also used generally for VB.NET routines, so distinguish a Sub from a Function by its declaration and return contract.

VB.NET
Module Program
    Sub PrintHeading()
        Console.WriteLine("Attendance")
    End Sub

    Sub Main()
        PrintHeading()
        Console.WriteLine("Monday")
        PrintHeading()
    End Sub
End Module

Control starts in Main. The first call prints Attendance, returns, then Monday is printed. The second call prints Attendance again. The code is written once but used twice. A call can also be written with the optional Call keyword; it is not needed here.

Subroutines support decomposition: a large problem becomes manageable jobs with meaningful names. Each job can be tested separately, reused with different data and assigned to a different programmer under an agreed interface. A correction to one shared definition repairs all its calls. These benefits depend on sensible boundaries: dividing every single statement into its own routine can make a program harder to follow.

Parameter interfaces

A routine's interface is the agreement between caller and routine: its name, parameters, types, passing mechanisms and any returned type. Here interface means that call contract, not a VB.NET Interface declaration for object-oriented programming.

The declaration contains formal parameters; a call supplies arguments. In AddBonus(score, 3), score and 3 are arguments. Their positions and types must match the declaration. Data flows explicitly across the boundary instead of being read from unrelated global variables.

VB.NET
Module Program
    Sub AddBonus(ByVal original As Integer, ByRef updated As Integer)
        original += 2
        updated += original
    End Sub

    Sub Main()
        Dim score As Integer = 5
        Dim total As Integer = 10
        AddBonus(score, total)
        Console.WriteLine(score)
        Console.WriteLine(total)
    End Sub
End Module

ByVal copies the argument's value into the parameter. Here original changes from 5 to 7 without changing score. ByRef allows the routine to update the caller's variable; updated adds 7 to total. The outputs are therefore 5 and 17. VB.NET defaults to ByVal if no mechanism is written, but explicit declarations make the contract easier to inspect.

Passing a reference type ByVal copies its reference, not the object. An array passed ByVal can still have an element changed; both references reach the same array. Assigning a new array to that ByVal parameter changes only the local reference. ByRef can additionally replace the caller's array variable. Do not confuse a reference type with the ByRef passing mechanism.

Continue this lesson.

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

Create free account