Search code examples
vb.netfunctionargumentsprocedures

How do I fix this VB code? [Argument not specified for parameter]


I'm struggling to get this code to work:

Module Module1 
    Function BMI(ByVal H, ByVal W)
        BMI = (W / H) ^ 2
        Return BMI
    End Function
    Function reading(ByVal BMI)
        If BMI <= 19 Then
            reading = "Underweight"
        ElseIf BMI >= -20 <= 25 Then
            reading = "Perfect"
        ElseIf BMI >= 26 <= 30 Then
            reading = "Slightly Overweight"
        ElseIf BMI >= 31 <= 40 Then
            reading = "Overweight"
        ElseIf BMI >= 41 Then
            reading = "Obese"
        End If
        Return reading
    End Function
    Sub Main()
        Dim height, weight As Integer
        Console.Write("What is your height? ")
        height = Console.ReadLine
        Console.Write("What is your weight? ")
        weight = Console.ReadLine
        BMI(height, weight)
        reading(BMI)
    End Sub
End Module

I'm informed that 'reading(BMI)' has an 'argument not specified for parameter 'W' of 'Public Function BMI(H As Object, W As Object) As Object.'

Please can someone help me fix this?


Solution

  • Here's your code re-factored a bit. It doesn't have any error checking on the user inputs. If you're working in metric units remove the * 703 part in the formula:

    Module Module1
    
        Sub Main()
            Dim height, weight As Integer
            Console.Write("What is your height in inches? ")
            height = Console.ReadLine
            Console.Write("What is your weight in pounds? ")
            weight = Console.ReadLine
    
            Dim BMI As Integer = CalculateBMI(height, weight)
            Dim Category As String = reading(BMI)
    
            Console.WriteLine(String.Format("Your BMI is {0}, which is {1}.", BMI, Category))
            Console.Write("Press Enter to Quit")
            Console.ReadLine()
        End Sub
    
        Function CalculateBMI(ByVal H As Integer, ByVal W As Integer) As Integer
            Return CDbl(W * 703) / Math.Pow(H, 2)
        End Function
    
        Function reading(ByVal BMI As Integer) As String
            Select Case BMI
                Case Is <= 19
                    Return "Underweight"
                Case 20 To 25
                    Return "Perfect"
                Case 26 To 30
                    Return "Slightly Overweight"
                Case 31 To 40
                    Return "Overweight"
                Case Else
                    Return "Obese"
            End Select
        End Function
    
    End Module