Functions with multiple output?

ndeans

Registered User.
Local time
Today, 20:50
Joined
Jun 5, 2006
Messages
39
Hi all,

I'm currently writing some fairly lengthy functions for a project to make things more efficient, but a lot of what i need to do can get quite repetitive.

I would love to be able to create a function that had more than a single output.

For example: (simplified)
Code:
WHR = CDec(Nz(waist)) / CDec(Nz(hip))
    
    If Gender = "Male" Then
        If WHR < 0.9 Then
            WHRRating = "Ideal"
            Else
            If WHR >= 0.9 And WHR < 1 Then
                WHRRating = "Moderate Risk"
                Else
                WHRRating = "High Risk"
                End If
            End If
        Else
        If WHR < 0.8 Then
            WHRRating = "Ideal"
            Else
            If WHR >= 0.8 And WHR < 0.9 Then
                WHRRating = "Moderate Risk"
                Else
                WHRRating = "High Risk"
                End If
            End If
        End If

Currently i have two values being created WHR and WHRRating. Is it possible to create a single function that can output the values WHR and WHRRating as having to create two seperate functions (1 for each) gets a little repetitive and seems innefficient to me?
 
Nope, functions only return a single value. So, you can split it up into the pieces you need like WHR first and then you can pass the WHR value to the WHRRating query:

Function WHRRating(decWHR As Decimal) As String
...etc.
 
A Function can only return one value but that value can be a pointer to a structure: -

Code:
Option Explicit
Option Compare Text


Public Type udtWHRRisk
    Gender    As String
    Waist     As Double
    Hip       As Double
    WHR       As Double
    WHRRating As String
End Type


Sub TestIt()
    Dim udtThisRisk As udtWHRRisk

    With udtThisRisk
        .Gender = "Male"
        .Waist = 36.5
        .Hip = 48.5
    End With
    
    With GetRisk(udtThisRisk)
        MsgBox .WHR & "  >>>  " & .WHRRating
    End With

End Sub


Public Function GetRisk(ByRef udtRisk As udtWHRRisk) As udtWHRRisk

    With udtRisk
        GetRisk.WHR = .Waist / .Hip
    
        If .Gender = "Male" Then
            Select Case GetRisk.WHR
                Case Is < 0.9
                    GetRisk.WHRRating = "Ideal"
            
                Case Is < 1
                    GetRisk.WHRRating = "Moderate Risk"
                    
                Case Else
                    GetRisk.WHRRating = "High Risk"
                    
            End Select
        Else
            [b]Select Case GetRisk.WHR[/b]
                Case Is < 0.8
                    GetRisk.WHRRating = "Ideal"
                
                Case Is < 0.9
                    GetRisk.WHRRating = "Moderate Risk"
                    
                Case Else
                    GetRisk.WHRRating = "High Risk"
                    
            End Select
        End If
    End With

End Function

More reading at UtterAccess…
http://www.utteraccess.com/wiki/index.php/Pass_And_Return_Variables


Edit
Incorrect code:
Changed…
Select Case .WHR
to
Select Case GetRisk.WHR

Regards,
Chris.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom