Skip to content

NonReturningFunctionInspection

Mathieu Guindon edited this page Feb 23, 2015 · 2 revisions

Description: Function does not return anything

Type: CodeInspectionType.CodeQualityIssues

Default severity: CodeInspectionSeverity.Warning

This inspection finds function procedures that don't assign their return value.

###Example:

Function DoSomething below seems to return an Integer, but because it is never assigned, it will always return 0:

Public Function DoSomething() As Integer
    Dim foo As Integer
    foo = 42
    DoSomethingElse foo
End Sub

This code is confusing, because a maintainer would expect DoSomething to return an Integer. Either the function is accidentally unassigned, or the function should be a Sub.


###QuickFixes

QuickFix: Convert function to procedure

Public Sub DoSomething()
    Dim foo As Integer
    foo = 42
    DoSomethingElse foo
End Sub

By making the unassigned function a Sub procedure, there is no more ambiguity on whether the function was accidentally unassigned, or if it was meant to be a Sub.

IMPORTANT Functions consumed by Microsoft Access macros must be Function procedures; when a VBA function is used by a Microsoft Access macro, applying this quickfix can break macros. Assign the function an error code instead (like DoSomething = 0) - Rubberduck will no longer mark the function as non-returning, and functionality will be preserved.