typestar

Subs and Functions in Visual Basic

The two callables: Functions return, Subs act.

Report("small", CircleArea(1.0))
Report("large", CircleArea(4.5))

Dim sizes() As Double = {1.0, 2.0, 3.0}
Dim areas = sizes.Select(AddressOf CircleArea).ToArray()
Console.WriteLine($"summed: {areas.Sum():F2}")

Function CircleArea(radius As Double) As Double
    Return Math.PI * radius * radius
End Function

Sub Report(label As String, value As Double)
    Console.WriteLine($"{label}: {value:F2}")
End Sub

How it works

  1. Function ... As Double declares the return type after the parameters.
  2. A Sub returns nothing — it exists for its side effect.
  3. AddressOf passes a named method where a lambda would go.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
401
Tokens
101
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 71 seconds.

Type this snippet

Step 1 of 3 in Subs & functions, step 13 of 29 in Language basics.

← Previous Next →