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
Function ... As Doubledeclares the return type after the parameters.- A
Subreturns nothing — it exists for its side effect. AddressOfpasses a named method where a lambda would go.
Keywords and builtins used here
CircleAreaDimDoubleEndFunctionReportReturnStringSub
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.
Step 1 of 3 in Subs & functions, step 13 of 29 in Language basics.