0

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?

Spooky
  • 2,966
  • 8
  • 27
  • 41
Ryan Grainger
  • 21
  • 1
  • 5
  • 2
    ypou have BMI as a function which takes 2 args. `reading(BMI(height, weight))` should work, but you will get another error/bad results. `ElseIf BMI >= -20 <= 25 Then` is not how to construct an IF statement – Ňɏssa Pøngjǣrdenlarp Feb 03 '15 at 16:59
  • 5
    Please use `Option Strict On`. – OneFineDay Feb 03 '15 at 17:00
  • 2
    You need to clean up the code and use well-thought names. Using the same name for a function and a parameter of another function is a bad idea, as you found out – Panagiotis Kanavos Feb 03 '15 at 17:00
  • 2
    I implore you to look at .NET naming conventions. https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx – TyCobb Feb 03 '15 at 17:03

4 Answers4

1

In order to hand over the result of the BMI-function to the reading method, you need to store the result in a variable instead of using the function name BMI again (the relevant changes are contained in the last two lines):

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
    Dim result = BMI(height, weight)
    reading(result)
End Sub
Markus
  • 20,838
  • 4
  • 31
  • 55
1

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
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

There were several things wrong. Try what I have below and see if it works as you were expecting. I commented up the code as to the changes I made.

Module Module1
    Function BMI(ByVal H As Integer, ByVal W As Integer) As Integer   ' set a return type and specify the parameter types
        BMI = (W / H) ^ 2
        ' Dont need this here, you can return the value by simply assinging the function name the return value
        ' Return BMI 
    End Function
    Function reading(ByVal _BMI As Integer) As String   ' set a return type and specify the parameter types
        'This was not the correct syntax for an If statement.
        If _BMI <= 19 Then
            reading = "Underweight"
        ElseIf _BMI >= 20 And _BMI <= 25 Then   ' ElseIf BMI >= -20 <= 25 Then   ' Also killed the -20 and made it just 20
            reading = "Perfect"
        ElseIf _BMI >= 26 And _BMI <= 30 Then   ' ElseIf BMI >= 26 <= 30 Then
            reading = "Slightly Overweight"
        ElseIf _BMI >= 31 And _BMI <= 40 Then   ' ElseIf BMI >= 31 <= 40 Then
            reading = "Overweight"
        ElseIf _BMI >= 41 Then
            reading = "Obese"
        Else
            reading = "Unknown"    ' Just in case...
        End If
        ' Again, don't need this here
        ' Return reading

    End Function
    Sub Main()
        Dim height, weight As Integer
        Try
            Console.Write("What is your height? ")
            height = Integer.Parse(Console.ReadLine) ' make sure it's an integer
        Console.Write("What is your weight? ")
            weight = Integer.Parse(Console.ReadLine) ' make sure it's an integer 

            Dim result As String = reading(BMI(height, weight)) ' Need to feed the reading function an actual value. Before it was trying to call the BMI function with no parameters hence why it was failing.
            Console.Write(String.Format("Your result is: {0}", result)) ' Output 
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub

End Module
-1

In addition to what Markus wrote:

Remove the "return" statements in the functions themselves .. I do not believe vba likes those there. You've assigned to the value to the function name .. that's all you need to do:

so remove:

Return BMI
Return reading
Ditto
  • 3,256
  • 1
  • 14
  • 28
  • 2
    The question is tagged with vb.net and I see nothing in the code that suggests it is vba. In any case, removing the return statements will not affect the error that the question is about. – Blackwood Feb 03 '15 at 17:23
  • Funny, I tested on my side, reproduced the error, removed the return, error went away .. *shrug* . seems like that was one of his problems. I didn't see any point in repeating what Markus had put ... marking me down for pointing out an error he's got but hasn't yet realized? how very odd. – Ditto Feb 03 '15 at 18:23
  • I have also tested. With `Option Strict On`, The code in the original question gets 16 build errors (and it would be good for the OP to address that). With `Option Strict Off`, there are two errors, both are "argument not specified" errors and neither go away if the return statements are removed. – Blackwood Feb 03 '15 at 19:05
  • And, of course, I should add that Return is a perfectly valid statement in vb.net. In fact it is the standard way for a Function to return a value. Using the Function name as a return variable is also supported for the benefit of those more familiar with VB 6 and earlier. – Blackwood Feb 04 '15 at 00:32