Flow-control structures: Difference between revisions

Content added Content deleted
Line 570: Line 570:


''This style of code is rarely used.''
''This style of code is rarely used.''

=== Return / Exit Sub ===

This shows the classical and modern syntax for exiting a sub routine early.

Sub Foo1()
If Not WorkNeeded() Then Exit Sub
DoWork()
End Sub
Sub Foo2()
If Not WorkNeeded() Then Return
DoWork()
End Sub

=== Return value / Exit Function ===

This shows the classical and modern syntax for exiting a function early. There is an implied variable with the same name as the function. This variable is write-only.

Function Foo3()
Foo3 = CalculateValue()
If Not MoreWorkNeeded() Then Exit Function
Foo3 = CalculateAnotherValue()
End Function
Function Foo4()
Dim result = CalculateValue()
If Not MoreWorkNeeded() Then Return result
Return CalculateAnotherValue()
End Function